C++ array as parameter - why do you only need to specify "outer" dimension C++ array as parameter - why do you only need to specify "outer" dimension arrays arrays

C++ array as parameter - why do you only need to specify "outer" dimension


but isn't a bidimensional array basically an array of arrays?

It is.

But the compiler needs to know the size really just in order to be able to perform pointer arithmetic correctly when indexing into the array (well, apart from allocation of course, but we are speaking in the context of functions here), since multidimensional arrays are continuous in memory. int arr[2][3] declares that there be 3 ints in a row, and two pieces of 3-int rows follow each other.

Now what happens to arrays when you pass them to a function is that they decay into a pointer. But it's only logical that the first (innermost) dimension decays into one, because we can index (in theory) an arbitrarily long array using a a single pointer to its first element.

If, however, there are multiple dimensions, then the compiler needs those dimensions so that it can perform pointer arithmetic on further dimensions.

Here is something you should read in addition.


When you pass an array parameter to a function, the parameter gets converted to a pointer to the first element of an array.

A two-dimensional array in C++ is really an array of arrays. So the parameter you're passing is a pointer to the array containing the first column.

To access the second column, the pointer must be incremented past the first column. To do that the compiler must know the size of the column, just as it must know the size of any other array element.


You actually need to specify all dimensions besides the first one. The reason is that the compiler won't know how much memory to allocate otherwise.

For a detailed answer, see the following question:

https://stackoverflow.com/a/2562111/1822214