GATE / CS ARCHIVE

GATE CSE 2010 / QUESTION 11

Pointer parameters passed by value

Programming in C · Pointers & Memory · Full GATE CSE 2010 paper

GATE 2010, Q11 1 mark · MCQ
Report issue

Pointers & Memory

Question 11, GATE 2010: What does this C program print?
#include <stdio.h>
void f(int *p, int *q) { p=q; *p=2; }
int i=0, j=1;
int main(void) { f(&i,&j); printf("%d %d\n",i,j); return 0; }
Options: (A) 2 2; (B) 2 1; (C) 0 1; (D) 0 2.
Tap to enlarge
Your answer
Answer & solution

Answer: D (no official key was published)

Initially i=0 and j=1. The local parameter assignment p=q redirects p to j, without altering i or the caller’s pointer arguments. Then *p=2 writes j=2. The printed values are 0 2, option D.

C passes the pointer values by value. At entry p contains &i and q contains &j; p=q copies &j into the local pointer p. It does not copy j into i. The later dereference acts on the new pointee j. Distinguish p=q, which redirects a pointer, from *p=*q, which would copy an integer.

Original paper, page 3