ProgrammingHardTCS NQT · Infosys pattern

Assume a 64-bit LP64 ABI. What is printed?

Assume a 64-bit LP64 ABI. What is printed?
struct Point { int x, y; };
struct Point origin = {0, 0};
struct Point *p1 = &origin;
struct Point *p2 = &origin;
p1->x = 5;
printf("%d %d", p2->x, origin.x);
  1. A. 0 0
  2. B. 5 0
  3. C. 0 5
  4. D. 5 5

Correct answer: D. 5 5

p1 and p2 both point at the SAME struct, origin; there is no copying involved here, just two pointers to one object. Modifying *p1's x field directly modifies origin's own x, and p2 (pointing at that same memory) sees the update too: both read 5.

More C Programming questions

Exams that ask this topic