1

I need to pass array of structures to a function and I make it this way:
it's my function

void ShowRoutes(Route *routeList, int n, string title) { //... }

and it's function call

ShowRoutes(routeList, n, "Unsorted list: ");

but I would like to know are there other ways to pass array of structures to function?

6
  • 7
    I'd recommend std::array or std::vector, depending on when you know the size. Commented Oct 16, 2013 at 11:37
  • I have just started learning C++, I don't know std::array or std::vector yet. Commented Oct 16, 2013 at 11:40
  • 1
    They're typically easier to deal with than plain arrays (and especially dynamic arrays). They're a worthy investment. Commented Oct 16, 2013 at 11:41
  • If you just started, you may as well learn some good practices now. It's worth the time. Commented Oct 16, 2013 at 11:41
  • 1
    Instead of passing data around, encapsulate it and perform operations on it. Commented Oct 16, 2013 at 12:07

1 Answer 1

1

Two ways I know of to pass arrays to functions.

The way you have it:

void ShowRoutes(Route *routeList, int n, string title) { //... }

or

void ShowRoutes(Route routeList[], int n, string title) { //... }

Either way you write the function, you would still call it the same way:

ShowRoutes(routeList, n, "Unsorted list: ");

But as others mentioned, would be a good idea to learn std::array and std::vector.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.