GATE Computer ScienceHardGATE CS (PYQ pattern)

Assume sizeof(int) = 4 and sizeof(int *) = sizeof(int (*)[4]) = 8. int a[3][4]; /* a[i][j] = 4*i + j */ int (*r)[4] = a + 1; int *p = *(r + 1) + 1; printf("%d %d %zu %zu", (*r)[3] + p[2], *(*(r - 1) + 2) * p[-1], sizeof(*r) + sizeof(r), sizeof(a) / sizeof(*a)); What is printed?

  1. A. 18 16 12 3
  2. B. 18 16 24 3
  3. C. 18 16 24 12
  4. D. 15 10 24 3

Correct answer: B. 18 16 24 3

r points to row 1, so (*r)[3] = a[1][3] = 7. *(r + 1) is row 2 and p = &a[2][1], so p[2] = a[2][3] = 11 and p[-1] = a[2][0] = 8; *(*(r - 1) + 2) = a[0][2] = 2. Sum 18, product 16. *r is a whole row (16 bytes) while r itself is a pointer (8): 24. sizeof(a) / sizeof(*a) = 48 / 16 = 3 rows, not 12 ints.

More Programming and Data Structures (GATE) questions

Exams that ask this topic