GATE / CS ARCHIVE

GATE CSE 2016 SET 1 / QUESTION 15

GATE CSE 2016 Set 1 Q15

Programming in C · Pointers & Memory · Full GATE CSE 2016 Set 1 paper

GATE 2016 Set 1, Q15 1 mark · NAT
Report issue

Pointers & Memory

Question 15, GATE 2016: Find the printed value in this C program (stdio.h is included):
void mystery(int *ptra,int *ptrb){int *temp;temp=ptrb;ptrb=ptra;ptra=temp;}
int main(){int a=2016,b=0,c=4,d=42;mystery(&a,&b);if(a<c)mystery(&c,&a);mystery(&a,&d);printf("%d\n",a);}
Tap to enlarge
Answer & solution

Official answer: 2016.0 : 2016.0

Our answer: 2016

mystery swaps only its local pointer parameters; it never writes through either pointer. All pointed-to integers remain unchanged and the conditional a<c is false. The printed a is 2016.

C passes the pointer values by value, producing local copies of the addresses. Changing which address ptra or ptrb contains does not modify a,b,c or d. To swap integers the function would need assignments through *ptra and *ptrb; to swap caller pointer variables it would need a different interface. After the first call a remains 2016, so 2016<4 is false. The final call likewise has no effect on a before printing.

Original paper, page 8 · Answer key