user695652
user695652

Reputation: 4275

Addresses of Array

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

Answers (3)

Mysticial
Mysticial

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

Bolivar Stephen
Bolivar Stephen

Reputation: 56

Because operator & take precedence over operator +.

Upvotes: 0

K-ballo
K-ballo

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

Related Questions