The GATE Grind

GATE 2010 CS (CS1) – Question 21

Programming and Data Structures · Programming in C · 1 mark · Multiple choice

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;
}
  1. 2 2
  2. 2 1
  3. 0 1
  4. 0 2

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.

More Programming in C questions

All Programming in C questions →