#include <stdio.h>
void foo(int *p, int x){
*p=x;
}
int main(){
int *z;
int a = 20, b = 25;
z = &a;
foo(z,b);
printf("%d",a);
return 0;
}The output of the given C program is __________. (Answer in integer)
#include <stdio.h>
void foo(int *p, int x){
*p=x;
}
int main(){
int *z;
int a = 20, b = 25;
z = &a;
foo(z,b);
printf("%d",a);
return 0;
}The output of the given C program is __________. (Answer in integer)
ANSWER
The output of the program is 25.
Official answer: 25Source: Official answer key, GATE 2025 CS1 (IIT Roorkee)
Declarations: int *z; and int a = 20, b = 25; set up an uninitialized pointer z and two ints, a = 20 and b = 25.
z = &a; makes z hold the address of a.
foo(z, b) is called: the parameter p receives the value of z (i.e., the address of a), and the parameter x receives the value of b (25) — a copy, not a reference.
Inside foo, *p = x; dereferences p (which points to a) and stores the value of x (25) into that memory location, i.e., into a.
Because p and z both hold the address of a, this write is visible in main: a is now 25 (its original value 20 is overwritten). b remains unchanged at 25 but plays no further role.
foo returns; printf("%d", a); prints the current value of a, which is 25.
Output: 25.
KEY IDEA
foo receives a pointer p and a value x. Since p is a copy of z (i.e., p also holds the address of a), the statement *p = x writes through the pointer directly into a in main, overwriting it with the value of x (a copy of b).
WATCH OUT FOR
Confusing pass-by-value with pass-by-reference: assuming that because b (value 25) is passed by value, the assignment inside foo cannot affect the caller's variables — missing that it is the pointer z (holding &a) that is passed, and dereferencing it writes directly into a.
From the GATE 2025 CS1 paper. Every question of this paper · Download the full paper · All 11 Pointers & arrays questions, with notes