0

I want to pass a multidimensional array to a function. The problem is that the size of the array will only be known at runtime. I know the size of the "inner arrays". The unknown size is that of the "outer" array.

I have tried passing the array in my function prototype by specifying the unknown size as a variable of type int but this resulted in an error. My function prototype looked something like this:

float get_basket(float basket[number][5]);

number above is a global variable of type int.

2
  • 2
    Pass it as a double pointer. For safety, you can pass the dimensions as extra parameters. Like this int **myarray... In your function prototype, the compiler is probably trying to dereference the pointer where it should have a type specification. Commented Dec 5, 2013 at 6:03
  • Use std::vector. Use std::vector. Use std::vector.Use std::vector. Have I mentioned using std::vector already? No? You really, really, REALLY should use std::vector. Commented Dec 5, 2013 at 6:05

1 Answer 1

2

If the size of the array shall be known at run time then it is better to use vector or at least you should build the array dynamically through pointers.

As for the function argument, you can pass it as:

float get_basket(int basket[][5]);

or

float get_basket(int **basket);
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.