GATE / CS ARCHIVE

GATE CSE 2025 SET 1 / QUESTION 34

Following a pointer argument back to its object

Programming in C · Pointers & Memory · Full GATE CSE 2025 Set 1 paper

GATE 2025 Set 1, Q34 1 mark · NAT
Report issue

Pointers & Memory

Question 34, GATE 2025: C function foo(int *p,int x) executes *p=x. In main, a=20,b=25, pointer z is assigned &a, then foo(z,b) is called and a is printed. What integer is printed?
Tap to enlarge
Answer & solution

Official answer: 25

Approach: Distinguish copying an argument from modifying the object that a copied pointer designates.
Initially a contains 20 and b contains 25. The assignment z=&a makes z hold the address of a. On calling foo(z,b), the parameter p receives a copy of that same address, while x receives the value 25.
Inside foo, the expression *p is the integer object at p’s address, which is main’s a. Therefore *p=x stores 25 into a. Returning from foo discards its local parameter objects but does not undo the write to a.
The subsequent printf of a prints 25. C still passes arguments by value: p itself is copied, but the copied address can be dereferenced to change an object owned by the caller. If the function had assigned only p or x, that would be a different operation; the dereferencing assignment is decisive.

Original paper, page 34 · Answer key