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?
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
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With