ProgrammingModerateTCS NQT · Wipro

What is printed?

What is printed?
#include <stdio.h>
struct P { int a; int b[3]; };
int main(void) {
    struct P arr[2] = {{1, {2, 3, 4}}, {5, {6, 7, 8}}};
    printf("%d %d", *(arr[0].b + 2), *arr[1].b);
}
  1. A. 3 6
  2. B. 4 5
  3. C. 2 5
  4. D. 4 6

Correct answer: D. 4 6

arr[0].b + 2 points at b[2] = 4, and arr[1].b points at b[0] of the second struct, which is 6. Off-by-one indexing gives 3, and mixing up the structs gives 5.

More C Programming questions

Exams that ask this topic