ProgrammingHardHCL · Tech Mahindra pattern

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

Assume a 64-bit LP64 ABI. What is printed?
struct Point { int x, y; };
void move(struct Point *p, int dx, int dy) {
    p->x += dx;
    p->y += dy;
}
int main(void) {
    struct Point pt = {1, 2};
    move(&pt, 10, 20);
    printf("%d %d", pt.x, pt.y);
}
  1. A. 1 2
  2. B. 11 22
  3. C. Compile-time error: -> cannot be used on a struct pointer parameter
  4. D. Undefined behaviour: pt is passed by reference unintentionally

Correct answer: B. 11 22

&pt passes the address of the actual struct, and p->x is shorthand for (*p).x, correctly reaching and modifying the caller's real pt through the pointer. After move(), pt.x = 1+10=11 and pt.y = 2+20=22, and these changes are visible in main because a pointer (not a copy of the struct) was passed.

More C Programming questions

Exams that ask this topic