ProgrammingHardTCS NQT · Infosys pattern

What is printed?

What is printed?
int a[2][3] = {{1,2,3},{4,5,6}};
int *rowptrs[2] = {a[0], a[1]};
int **pp = rowptrs;
for (int i = 0; i < 2; i++)
    for (int j = 0; j < 3; j++)
        pp[i][j] *= 2;
printf("%d %d %d %d", a[0][0], a[0][2], a[1][0], a[1][2]);
  1. A. 4 6 8 12
  2. B. 1 3 4 6
  3. C. 2 4 6 8
  4. D. 2 6 8 12

Correct answer: D. 2 6 8 12

rowptrs[0] and rowptrs[1] alias a's actual rows (not copies), so doubling through pp doubles a in place: a[0][0]=2, a[0][2]=6, a[1][0]=8, a[1][2]=12.

More C Programming questions

Exams that ask this topic