[C++] Multi-dimensional Array Argument

Started by
6 comments, last by SiCrane 17 years, 9 months ago
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.
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
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[])
Those aren't multidimensional arrays.
Why would you say that SiCrane?
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.
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.

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.

This topic is closed to new replies.

Advertisement