Question
#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)
Official answer and solution
- Official answer
- 25
- From
- Official GATE 2025 answer key
Solution
Trace how passing pointer z = &a to foo modifies a via dereferencing *p = x.
- In
main(),ais initialized to20,bto25, and pointerzis assigned the address ofa(z = &a). - Calling
foo(z, b)passesp = &aandx = 25. - Inside
foo,*p = x;writes25directly to the memory location ofa. printf("%d", a);prints 25.
Final answer
25
The answer is the one in the official GATE answer key. The solution is this site's own explanation.
Source
- Paper
- GATE 2025 Computer Science and Information Technology - Session 1 (CS1)
- Question
- 34 of 65
- Section
- Computer Science and Information Technology
Report an error in this question
GATE questions are the work of the organising institutes. This site is independent and is not affiliated with them.