ProgrammingHardWipro · Cognizant pattern

Assume a 64-bit LP64 ABI. What is printed?

Assume a 64-bit LP64 ABI. What is printed?
int a[2][3] = {{1, 2, 3}, {4, 5, 6}};
int *p = (int *)a;
for (int i = 0; i < 6; i++) printf("%d", p[i]);
  1. A. 142536
  2. B. 654321
  3. C. Undefined behaviour: casting a 2D array to int* is illegal
  4. D. 123456

Correct answer: D. 123456

A 2D array like int a[2][3] is guaranteed to be laid out as 6 contiguous ints in row-major order; casting its address to int* and walking it linearly with p[i] simply visits 1,2,3,4,5,6 in that storage order, which is a well-defined (if slightly unusual) technique.

More C Programming questions

Exams that ask this topic