Is
*(ary[3]+8)
and
ary[3][8]
are the same ? If yes, please explain how ? ary[3] returns the address of first element or the value in ary[3][0] ? ary is a two dimensional array.
Thanks in advance.
Yes
a[i] is same as *(a+i)
ary[i][j] is same as *( *(ary+i)+j))
a[i][j] is equivalent to *(*(a+i) + j). For a true 2D array, this is also equivalent to *((T*)a + 3*i + j) (where T is the type of each element).T a[M][N], then *(a+i) is of type T[N], which decays to T* in most situations.*(ary[3]+8) says value at 8th column of third row.ary[3] is base address of third Row.ary[3][8] will also access to same element at third row and 8th column.
For Example i am taking an 2D array of two row and 4 column which is equivalent to 1D array of 8 elements.As shown below.
int a[8] = {0,1,2,3,4,5,6,7};
int b[2][4] = {{0,1,2,3},{4,5,6,7}};
since b is 2D array , so you can consider it as array of two 1D arrays.when you pass b[1] or b[1][0] it says address of first row.Rectangular array allocated in memory by Row.so if you want to find address of element a[row][col] it will get calculated as
address = baseAddress + elementSize * (row*(total number of column) + col);
ary[i] are not stored anywhere if your array is declared as e.g. int ary[4][9]. The compiler knows that your ary is a two-dimensional array, and when you do ary[i] this gives the address you say. But note that the following example is completely different case: int *ary[4]; int ary1[9]; int ary2[9]; int ary3[9]; int ary4[9]; ary[0] = ary1; ary[1] = ary2; ary[2] = ary3; ary[3] = ary4; although you can indeed call this a 2d array as well. Here, the pointers to the second dimension are explicitly stored.As others already have said, a[i] is just a sugar for *(a+i).
I just would like to add that it always works, that allows us to do things like that:
char a[10];
char b;
char c[10][20];
// all of these are the same:
b = a[5]; // classic
b = *(a + 5); // pointer shifting
b = 5[a]; // even so!
b = c[5][9];
b = *(c[5] + 9);
b = *(*(c + 5) + 9);
b = *(c + 5)[9];
b = 5[c][9];
b = 5[9][c]; // WRONG! Compiling error
*(*(ary+ 3) + 8). Note thatary[3]is the same as*(ary + 3). This should answer your question.