ProgrammingVery hardProduct company OA

What is printed? Assume a 64-bit LP64 ABI (4-byte int) and that the matrix is stored contiguously.

What is printed? Assume a 64-bit LP64 ABI (4-byte int) and that the matrix is stored contiguously.
int a[2][3] = {{1,2,3},{4,5,6}};
int *p = (int*)a;
printf("%d %d %d", *(p+4), *(*(a+1)+2), *(a[0]+4));
  1. A. 5 6 5
  2. B. 5 6 4
  3. C. 4 6 5
  4. D. 5 5 5

Correct answer: A. 5 6 5

Flattening the 2x3 matrix, p+4 lands on the 5th int, which is a[1][1]=5. *(a+1)+2 is &a[1][2], value 6. a[0]+4 walks past row 0's bound into row 1's storage (contiguous, though technically out of the row's own bounds) landing on a[1][1]=5 again.

More C Programming questions

Exams that ask this topic