ProgrammingHardProduct company OA

What is printed?

What is printed?
struct P { int x; int y; };
struct P a[3] = {{1, 2}, {3, 4}, {5, 6}};
struct P *p = a;
int r1 = (p + 2)->x;
int r2 = (++p)->y;
int r3 = p[1].x - p[-1].y;
printf("%d %d %d", r1, r2, r3);
  1. A. 5 4 1
  2. B. 3 4 3
  3. C. 5 2 3
  4. D. 5 4 3

Correct answer: D. 5 4 3

p + 2 is &a[2], so r1 = 5 (p itself does not move). ++p moves p to a[1] before the access, so r2 = 4. Now p[1] is a[2] and p[-1] is a[0]: 5 − 2 = 3. '5 2 3' treats ++p like p++.

More C Programming questions

Exams that ask this topic