297 views

After executing following lines of C code, &arr[1][2] is same as:

int (*a)[3];
int arr[2][3];
a = arr+1;

1. &a[0][2]
2. (*a+2)
3. (int *)(&arr+1)-1
4. *(a+2)

 0988 0992 0996 1000 1004 1008 1012 1016 1020 1024 1028 1032 1036 1040 1044

Suppose (for visualization), int takes 4B and above is a snapshot of memory (addresses only).

int arr[2][3] → arr is a 2x3 array of int. suppose base address of arr is 1000 (arr = 1000). arr acts as a pointer to int array of size 3. &arr acts as a pointer to int array of size 2x3.

&arr[1][2] → this is address of arr[1][2]. &arr[1][2] points to int located at 1020.

{ &arr[1][2] ≡ *(arr + 1) + 2; arr + 1 = 1000 + 1 * sizeof(*arr) = 1012 and *(arr + 1) + 2 = 1012 + 2 * sizeof(int) = 1020 }

int (*a)[3] → a is a pointer to int array of size 3. a = arr + 1 = 1000 + 1 * sizeof(*arr) = 1012.

&a[0][2] → this is same as &(*(*(a + 0) + 2)), which is same as *a + 2. *a is an int pointer. Thus, the address equates to 1012 + 2 * sizeof(int) = 1020. (*a + 2) and &a[0][2] points to int located at 1020.

(int *)(&arr + 1) – 1 →  &arr + 1 equates to 1000 + 1 * sizeof(arr) = 1024. Now, (int *)(&arr+1) is pointer to int. Thus, (int *)(&arr+1) – 1 = 1024 – 1 * sizeof(int) = 1020. (int *)(&arr + 1) – 1 points to int located at 1020.

*(a + 2) → a + 2 = 1012 + 2 * sizeof(*a) = 1036. *(a + 2) points to int located at 1036.