GATE 2010 CS (CS1) – Question 21
What is printed?
#include <stdio.h>
void f(int *p,int *q) { p=q; *p=2; }
int i=0,j=1;
int main() {
f(&i,&j);
printf("%d %d",i,j);
return 0;
}Practise this question in The GATE Grind →
Show answer and explanation
Correct answer: (D) 0 2
Explanation
p initially points to i but is a local pointer variable. Assigning p=q redirects it to j without modifying i or the caller’s pointer values. The store *p=2 therefore changes j to two. Output is 0 2, D.