ProgrammingHardTCS NQT · Accenture

What is printed?

What is printed?
#include <stdio.h>
int main(void) {
    int a[3][4];
    int n = 0;
    for (int i = 0; i < 3; i++)
        for (int j = 0; j < 4; j++)
            a[i][j] = n++;
    int *p = &a[0][0];
    printf("%d %d", *(p + 6), *(*(a + 2) + 1));
}
  1. A. 6 9
  2. B. 8 9
  3. C. 6 8
  4. D. 5 9

Correct answer: A. 6 9

Element 6 in row-major order is a[1][2] = 6, because each row has four ints. Pointer arithmetic over a flat view uses the row length 4, and a[2][1] = 9.

More C Programming questions

Exams that ask this topic