ProgrammingVery hardProduct company OA

Assume a 64-bit LP64 ABI (4-byte int, 8-byte pointers and double, natural alignment). What is printed?

Assume a 64-bit LP64 ABI (4-byte int, 8-byte pointers and double, natural alignment). What is printed?
int a[3][4] = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}};
int (*p)[4] = a + 1;
int *q = *p + 2;
printf("%d %d %d %td %d %zu", (*p)[1], q[1], q[-2], q - a[1], p[1][0], sizeof *p / sizeof **a);
  1. A. 6 8 5 2 9 4
  2. B. 6 8 5 2 9 2
  3. C. 2 4 1 2 5 4
  4. D. 6 8 5 6 9 4

Correct answer: A. 6 8 5 2 9 4

a + 1 has type int (*)[4] and points at the whole second row, so (*p)[1] = 6 and p[1] is the third row (p[1][0] = 9). *p decays to &a[1][0], hence q = &a[1][2]: q[1] = 8, q[-2] = 5 and q − a[1] = 2 elements. sizeof *p is a full row, 16 bytes, and **a is one int, so the ratio is 4. The option ending in 2 confuses sizeof *p with the size of a pointer (8).

More C Programming questions

Exams that ask this topic