Alternative explanation (to matrix decay to pointer):
Let's say we have a one-dimensional array, and we use it as follows:
int array[10]; int i = array[3];
The compiler should know where to find array[3] . He knows that he needs to skip 3 int before he can get to the item array[3] . So it works.
But if we have a two-dimensional array,
int array[2][5]; int i = array[1][1];
To get i here, how many int do the compiler need to skip? He needs to skip the whole line, plus one. Skipping one is easy, since we know the size of one int . But we also need to know the row size in the & mdash array, and the row size is determined by the size * of the number of columns in the row. This is one way to look at it, which explains why you need the last dimension.
Letβs make it a little brain titration, taking it one size further,
int array[2][2][2]; int i = array[1][1][1];
and call the dimensions X, Y, Z.
Here we can say that we have a finite three-dimensional space int s. The unit, of course, has a size of 1 int . The number of lines is determined by Y, the number of planes is determined by Z. This leaves X as the base unit, the size of which is int , as we said. The combination of these three gives a "point".
To be able to get to any point in this three-dimensional space, we need to know where each dimension "stops" and the next begins. Therefore, we need:
- Unit Size (
int ) to move to size X - The size of each plane (Y) to cross the dimension of Y
- The number of planes to cross the size Z
So again, X is already provided to us, because we use int . But we do not know the size of each aircraft, and we do not know how many aircraft there are. Therefore, we need to specify everything except the first dimension. And this is a general rule.
It also explains why this problem offers a slightly more detailed explanation than just decaying a pointer, because as soon as you move on to more than two dimensions, you still need to know how it works. In other words, you need the total size (product sizes) so that it does not overflow, and you need the size of each size to be able to use sequential indexes [] .