Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can we omit the first dimension of a multidimensional array when we are passing it to a function

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?

like image 977
Mike Egren Avatar asked Nov 20 '11 22:11

Mike Egren


People also ask

Which dimensions can we omit when declaring a multidimensional array?

(For a three- or more dimensional array, all but the first dimension are required; again, only the first dimension may be omitted.)

Why do dimensions beyond the first in a multidimensional array in C++ have to be specified in the function header when they are passed a parameter?

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.

Is it must to specify all dimensions of a multidimensional array?

C) It is a must to specify all dimensions of a multidimensional array.


1 Answers

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).

like image 89
Michael Krelin - hacker Avatar answered Nov 16 '22 02:11

Michael Krelin - hacker