Why can we omit the first dimension of a multidimensional array when we are passing it to a function?
In my programming class, we were told what when passing a multidimensional array to a function we can omit first dimension, for example, a[10][15][20]
can be passed as a[][15][20]
.
Why?
(For a three- or more dimensional array, all but the first dimension are required; again, only the first dimension may be omitted.)
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.
C) It is a must to specify all dimensions of a multidimensional array.
Because the array will decay to pointer and to calculate offset to the elements of the array you do not need to know the innermost dimension. Offset to a[i][j][k]
is i*nj*nk+j*nk+k
(where nj
and nk
are corresponding dimensions).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With