ProgrammingHardTCS NQT · Wipro

What is printed?

What is printed?
#include <stdio.h>
struct P { int v; };
int main(void) {
    struct P arr[3] = {{1}, {2}, {3}};
    int sum = 0;
    for (struct P *p = arr; p < arr + 3; p++) p->v *= 2;
    for (int i = 0; i < 3; i++) sum += arr[i].v;
    printf("%d", sum);
}
  1. A. 6
  2. B. 9
  3. C. 12
  4. D. 8

Correct answer: C. 12

Each element is doubled through the pointer, so the values become 2, 4 and 6 with a sum of 12. Doubling only the first element gives 8.

More C Programming questions

Exams that ask this topic