GATE 2025 CS (CS1) – Question 34
#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)
Practise this question in The GATE Grind →
Show answer and explanation
Correct answer: 25
Explanation
Initially $a=20$ and $b=25$. The assignment `z = &a` makes `z` point to `a`.
Calling `foo(z,b)` copies that address into `p` and copies 25 into `x`. Inside `foo`, `*p=x` writes 25 into the object addressed by `p`, which is `a` in `main`.
Thus `printf` reads the updated value of `a` and prints **25**. Passing the pointer by value still allows the pointed-to object to be modified.