ProgrammingHardWipro · Cognizant pattern

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

Assume a 64-bit LP64 ABI. What is printed?
int mat[2][3] = {{1, 2, 3}, {4, 5, 6}};
int (*rowPtr)[3] = mat;
printf("%d %d %d %zu", (*rowPtr)[1], (*(rowPtr + 1))[2], **(rowPtr + 1), sizeof(rowPtr));
  1. A. 2 6 4 4
  2. B. 1 6 4 8
  3. C. 2 5 4 8
  4. D. 2 6 4 8

Correct answer: D. 2 6 4 8

rowPtr points at the whole first row. (*rowPtr)[1] is mat[0][1]=2. rowPtr+1 points at the second row; (*(rowPtr+1))[2] is mat[1][2]=6. **(rowPtr+1) dereferences twice, landing on mat[1][0]=4. sizeof(rowPtr) is just the size of a pointer, 8 bytes on LP64, not the size of what it points to.

More C Programming questions

Exams that ask this topic