GATE / CS ARCHIVE

GATE CSE 2017 SET 2 / QUESTION 14

GATE CSE 2017 Set 2 Q14

Programming in C · Pointers & Memory · Full GATE CSE 2017 Set 2 paper

GATE 2017 Set 2, Q14 1 mark · MCQ
Report issue

Pointers & Memory

Question 14, GATE 2017: Consider C code: void printxy(int x,int y){ int *ptr; x=0; ptr=&x; y=*ptr; *ptr=1; printf("%d,%d",x,y); }. What does printxy(1,1) print? A:0,0. B:0,1. C:1,0. D:1,1.
Tap to enlarge
Your answer
Answer & solution

Official answer: C

Step through it (10 steps)

1. At function entry the local parameter values are x=1,y=1. The assignment x=0 changes x, and ptr=&x makes ptr point to the storage occupied by x.

2. The expression *ptr reads that storage, currently 0, so y=*ptr copies 0 into y. This is a value copy, not a lasting connection between y and x.

3. The next statement *ptr=1 writes into x through the pointer. Now x=1 while y remains 0. The output is 1,0, option C. C function arguments are passed by value; the original call literals do not constrain these local assignments.

Original paper, page 6 · Answer key