• Advertisement
Sign in to follow this  

[C++] Multi-dimensional Array Argument

This topic is 4248 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I know that you can have a single dimensional array argument simply like this myFunction(int array[]) but, what about multidimensional? if I do this: myFunction(int array[][]) I get errors.

Share this post


Link to post
Share on other sites
Advertisement
You can only create a multidimensional array argument if all the dimensions except the first are specified (though specifying the first can also be done). i.e.:
void myFunction(int argument[][64]) // legal
void myFunction(int argument[][5][5]) // legal
void myFunction(int argument[][5][]) //illegal
void myFunction(int argument[2][2]) // legal

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Quote:
Original post by SiCrane
You can only create a multidimensional array argument if all the dimensions except the first are specified (though specifying the first can also be done). i.e.:
void myFunction(int argument[][64]) // legal
void myFunction(int argument[][5][5]) // legal
void myFunction(int argument[][5][]) //illegal
void myFunction(int argument[2][2]) // legal


and not forgetting

void myFunction(int** argument)
void myFunction(int* argument[])

Share this post


Link to post
Share on other sites
Those aren't multidimensional arrays.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Why would you say that SiCrane?

Share this post


Link to post
Share on other sites
int** is simply a pointer to a pointer. Consider:
void MyIllAdvisedFunctionToCreateAnArray(int** array, size_t size)
{
(*array) = new int[size];
}
As you can see this doesn't involve multi-dimensional arrays in any way. In other words, a multi-dimensional array is one possible interpretation of a pointer to a pointer, but it isn't the only interpretation.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Quote:
Original post by jyk
...a multi-dimensional array is one possible interpretation of a pointer to a pointer, but it isn't the only interpretation.


True, but the function prototypes are perfectly valid.

Share this post


Link to post
Share on other sites
Actually, multi-dimensional arrays have a specific type in the C and C++ type system, which is not compatible with the pointer to pointer type. That is to say there is no conversion from an int[2][2] to an int **. They just aren't the same.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement