GATE / CS ARCHIVE

GATE CSE 2016 SET 2 / QUESTION 12

GATE CSE 2016 Set 2 Q12

Programming in C · Functions & Parameter Passing · Full GATE CSE 2016 Set 2 paper

GATE 2016 Set 2, Q12 1 mark · NAT
Report issue

Functions & Parameter Passing

Question 12, GATE 2016: C-like code: void f(int* p,int m){m=m+5; *p=*p+m; return;} void main(){int i=5,j=10; f(&i,j); printf("%d",i+j);} Find the printed value under the intended ordinary integer execution.
Tap to enlarge
Answer & solution

Official answer: 30.0 : 30.0

Our answer: 30

The value parameter m changes from 10 to 15 locally. Writing through p updates i from 5 to 20, while caller j stays 10. Hence i+j=30. Routine library declarations and the intended void-main extension are assumed.

Passing &i copies i’s address, not its value, into p. Dereferencing p consequently changes the caller’s i. Passing j copies 10 into the separate local variable m, so changing m cannot change j. The final addition is 20+10, not 20+15 or the original 5+10.

Original paper, page 28 · Answer key