11

Why it is not necessary to mention first dimension of multidimensional array and necessary to mention other dimensions:

int A[][][2]={{{1,2},{3,4}},{{4,5},{5,6}}}; // error 
int A[][2][2]={{{1,2},{3,4}},{{4,5},{5,6}}}; // OK 

I am not able to understand the concept or logic behind this.

phuclv
  • 37,963
  • 15
  • 156
  • 475
Rishab Shinghal
  • 591
  • 3
  • 16
  • 3
    Can you show a code sample of what exactly you are talking please. – πάντα ῥεῖ Oct 25 '15 at 09:51
  • 1
    Usually, people are asking this question because they intend to pass a multidimensional array as parameter to a function and they would like the function to handle any size of array. If it's your case, I'd strongly recommend you use vectors instead. – Christophe Oct 25 '15 at 09:57
  • int A[][][2]={{{1,2},{3,4}},{{4,5},{5,6}}} here it gives error int A[][2][2]={{{1,2},{3,4}},{{4,5},{5,6}}} here it's fine – Rishab Shinghal Oct 25 '15 at 09:59
  • Are you asking specifically about the case where you declare an array and supply an initializer, or about multi-dimensional arrays in general? – n. m. could be an AI Oct 25 '15 at 10:50

3 Answers3

8

It is necessary to mention both dimensions of 2D arrays except when it is in function's parameter or if an initializer is present then first dimension can be omitted.

When used as a parameter in a function, for example,

int 2D_arr[m][n]   

converted to

int (*2D_arr)[n]  

Therefore, first dimension can be omitted. But, second dimension must be there to tell the compiler that the pointer 2D_arr is a pointer to an array of n ints.

In second case, when initializer is present

int A[][2][2]={{{1,2},{3,4}},{{4,5},{5,6}}};    

the compiler uses the length of the initializer to calculate the first dimension only. The rest of the dimension must be explicitly specified at the time of declaration.

haccks
  • 104,019
  • 25
  • 176
  • 264
  • 1
    I think he is asking why more dimensions cannot be deduced when initializers are present – M.M Oct 25 '15 at 10:05
7

Because when using a multidimensional array, computing the actual index uses all dimension sizes except the first. For example for a 3D array declared as int arr[3][4][5];, arr[i][j][k] is by definition *(&(arr[0][0][0]) + k + 5 *(j + 4 * i))

So when the first dimension can be deduced from the context initialization, or may be ignored (when getting a parameter in a funtion) it can be omitted.

Examples:

int arr[][2] = { 1,2,3,4 };

void setArr(void *container, int arr[][4]);
Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252
  • This is the fundamental point, but it might be clearer to say that dereferencing the array `arr[i][j][k]` is "equivalent to" `arr[i][j][k] = *(&(arr[0][0][0]) + k + 5 *(j + 4 * i));` rather than exhibiting it as part of the same code as the declaration because you don't have to write that kind of line in more applications. And if it was my answer I would name the array sizes because it is easy to read that way without reference back to the declaration. – dmckee --- ex-moderator kitten Oct 25 '15 at 15:36
  • @dmckee: you are right. I didn't realize it appeared as code... Post edited. – Serge Ballesta Oct 25 '15 at 15:40
1

If you declare a 2D array in a static way...

int arr[3][4];

... then its two dimensions are obvious.

If you declare a 2D array in a dynamic way, or as a pointer to pointers...

int r = 3, c = 4;
int** arr = new int*[r];
for (int i = 0; i < r; i++) {
    arr[i] = new int[c];
}

... it looks as if only one dimension is mentioned during allocation but that's because first you allocate the rows and then each column. When you get or set an element, you specify both dimensions as usual...

num = arr[1][2];
arr[1][2] = num;
dspfnder
  • 1,135
  • 1
  • 8
  • 13