Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 354
user695652 Avatar asked Nov 10 '11 20:11

user695652


2 Answers

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

like image 100
Mysticial Avatar answered Oct 24 '22 04:10

Mysticial


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

like image 35
K-ballo Avatar answered Oct 24 '22 05:10

K-ballo