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.

4
  • You can access also it like this: *(*(ary+ 3) + 8). Note that ary[3] is the same as *(ary + 3). This should answer your question. Commented Jul 2, 2013 at 7:08
  • The value at *(ary+3) and ary[3] are the same. What does *(ary+3) return ? The value at ary[3][0] or the starting address of ary[3] ? Commented Jul 2, 2013 at 7:10
  • @StoryTeller : ary is 2d array. Can you please share your knowledge on this ? Commented Jul 2, 2013 at 7:16
  • @SriniVas, see here stackoverflow.com/a/14111286/817643 about what defining a true 2d array means Commented Jul 2, 2013 at 9:03

4 Answers 4

4

Yes

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

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

Sign up to request clarification or add additional context in comments.

8 Comments

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 ?
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.
@StoryTeller: What is a counterexample?
@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).
@StoryTeller: If you have T a[M][N], then *(a+i) is of type T[N], which decays to T* in most situations.
|
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.

Comments

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

3 Comments

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?
@SriniVas Edited my answer for clarity.
@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.
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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.