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

arrays, c++

Solution

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 `int`s 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.

Problem

I'm trying to figure out multidimensional arrays and specifically how to fill them by passing them to functions. It's all very unintuitive, but the unintuitivest thing of all, which doesn't make any sense at all to me is: Why do you have to specify the number of columns, but not the number of rows when passing an 2d array as a parameter? I've probably looked at five or more forum threads that give the syntax, but none of them explained the reasoning behind it. I am okay with the compiler needing to know the size of an array to operate on it, but isn't a bidimensional array basically an array of arrays?

Original source

Related problems