Reputation: 4275
If I have an array declared like this:
int a[3][2];
stored at address A
.
Then a+1 is equal to A+2*4
, this is clear to me,
but why is &a+1
equal to A+6*4
?
Upvotes: 8
Views: 141
Reputation: 471509
a
is an array of int[2]
. Which has size 2 * sizeof(int)
. That's why a + 1 = A + 2*4
. (since sizeof(int) = 4
in your case)
However, &a
is a pointer to int[3][2]
. Since sizeof(int[3][2]) = 6 * sizeof(int)
, therefore: &a + 1 = A + 6*4
Upvotes: 7
Reputation: 81389
Then a+1 is equal to A+2*4
This is because a
decays to int (*)[2]
, and +1
results in 2 * sizeof(int)
.
but why is &a+1 equal to A+6*4?
In this case, &a
returns int (*)[3][2]
, and +1
results in 2 * 3 * sizeof(int)
.
Upvotes: 2