Public Group

# [C++] Multi-dimensional Array Argument

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

## 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 on other sites
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 on other sites
Quote:
 Original post by SiCraneYou 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]) // legalvoid myFunction(int argument[][5][5]) // legalvoid myFunction(int argument[][5][]) //illegalvoid myFunction(int argument[2][2]) // legal

and not forgetting

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

##### Share on other sites
Those aren't multidimensional arrays.

##### Share on other sites
Why would you say that SiCrane?

##### 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 on other sites
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 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.

1. 1
Rutin
38
2. 2
3. 3
4. 4
5. 5

• 11
• 10
• 13
• 104
• 11
• ### Forum Statistics

• Total Topics
632977
• Total Posts
3009677
• ### Who's Online (See full list)

There are no registered users currently online

×