3

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.

Srini Vas
  • 115
  • 1
  • 10

4 Answers4

4

Yes

a[i] is same as *(a+i)

ary[i][j] is same as *( *(ary+i)+j))

Maroun
  • 94,125
  • 30
  • 188
  • 241
banarun
  • 2,305
  • 2
  • 23
  • 40
  • Doesn't *(ary+i) in *(*(ary+i)+j)) return value at ary[i] ? Pointer holds an address and * is to retrieve the value at the address. Going by this, *(ary+i) has to return the value in ary+i. And we add j to the value at the address ary+i and change the value at the address obtained from the previous step. Isn't that how it has to work or works ? – Srini Vas Jul 02 '13 at 07:15
  • 1
    I do not see a contradiction in the previous Srini Vas's comment. He says pointer holds and address and * is to retrive the value at the address. The value, in this case, is a pointer to a (1dim) array. – ondrejdee Jul 02 '13 at 07:24
  • 3
    @StoryTeller: What is a counterexample? – Oliver Charlesworth Jul 02 '13 at 07:26
  • A true 2d array `a[2][3]`, when indexed, expands into `*(a + 2i + j)`. The size is part of the type. What banarun said is only true when `a` is a one dimensional array of pointers. – StoryTeller - Unslander Monica Jul 02 '13 at 08:58
  • 1
    @StoryTeller: It's *always* the case that `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). – Oliver Charlesworth Jul 02 '13 at 14:19
  • @OliCharlesworth, if that's the case, `*(a+i)` must be a pointer type. What type is it? – StoryTeller - Unslander Monica Jul 02 '13 at 14:53
  • 1
    @StoryTeller: If you have `T a[M][N]`, then `*(a+i)` is of type `T[N]`, which decays to `T*` in most situations. – Oliver Charlesworth Jul 02 '13 at 15:55
  • @OliCharlesworth, ignore the deleted comment. I removed it for a reason :) – StoryTeller - Unslander Monica Jul 02 '13 at 16:04
1

*(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);

Dayal rai
  • 6,548
  • 22
  • 29
  • Where/How in memory is the base array of ary[3] is stored? ary[0..n][0..n] are stored sequentially. ary[3] and ary[3][0] has to have the same base address and how is it resolved when invoked by calls of above types? – Srini Vas Jul 02 '13 at 07:22
  • @SriniVas Edited my answer for clarity. – Dayal rai Jul 02 '13 at 07:39
  • @SriniVas Aha, this is what you mean. The values of `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. – ondrejdee Jul 02 '13 at 07:48
1

If x is an array (int, say) x[i] is just a syntactic sugar for *(x+i). In your case, ary is a two-dimensional array (again of int, say). By the same syntactic sugar mechanism, ary[i][j] is equivalent to *((*(ary+i))+j), from which it is clear what happens under the hood.

ondrejdee
  • 476
  • 2
  • 8
0

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
Alexander Mihailov
  • 1,154
  • 7
  • 15