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?
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.
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
The simple answer is you need to think of array declaration in terms of pointers.
When you pass an array as an argument to a function, it will decay into a pointer to the first element of the array.
int intArray[3]
// Decays into
int * intArray;
Which are equivalent in the mind of the compiler. When you refer to an array by index, the compiler will actually use pointer arithmetic to determine which slot of memory to go to.
Now with your 2D array, it actually decays into a pointer to the first element as well. If the array is declared to be on the stack, the 2D storage is actually in Row Major format. This is something many people new to C++ overlook. The compiler needs to know the column value in order to perform pointer arithmetic to find the element you are looking for. C++ arrays are not objects and this my friend is why there are no length checks for arrays in C++.