5k views

What is printed by the following C program?

int f(int x, int *py, int **ppz)
{
int y, z;
**ppz += 1; z = **ppz;		 // corrected z = *ppz; to z = **ppz;
*py += 2; y = *py;
x += 3;
return x+y+z;
}

void main()
{
int c, *b, **a;
c = 4; b = &c; a = &b;
printf("%d", f(c, b, a));

}
1. $18$
2. $19$
3. $21$
4. $22$

edited | 5k views
0

Mistake is there. 0
that also has mistake rt?
0
yes Return $x+y+z =$ return $7 + 7 + 5 =$ return $19$

So, option B $= 19$ is correct.

edited
0
good explanation
–1
on execution of "x+=3;" the value returned should be 10+7+5=22.
x being an integer.
+4
X is  just a local variable of f, it is not same as C,( call by value) while calling the function f , c's value was passed to it, that's it.  Any changes to C's ( using its pointer variable)vàlue will not effect X 's value..
0
The main point to be careful above code is X=X+3, not X= *X+3
0
Can anyone explain the difference between f(c,b,a) & f(c,&b,&a).

Becuase in question it is pass by value i.e

f(c,b,a). How the actual argument is changing with out passing the address of the actual argument.
0
x is passed by value

so x's value should be 4  and answer should be 16 right?
0
I also want to know.. did you get a reason?
0
'b' is a pointer variable &  it is being assigned an address of 'c' & similarly 'a' is a double pointer variable because it is being used to store the address of another pointer variable. So when we call the function with them as parameters basically means we are passing addresses. The first parameter is a value & the other two are addresses, to store which the function is using similar datatypes  'py' & 'ppz'.

z = *ppz is a typo and it must be z = **ppz;

0
it will be *py ...
+1 vote
+1 vote

Answer is B 0
Its really appriciatable
z = *ppz Shouldn't be z = **ppz ??
+5

Yes. But you can't complain so in GATE. GATE takers are expected to apply logic to correct the questions since GATE 2013. Never say "mistake" in question paper- they are made by experts.

http://gate.iitm.ac.in/gateqps/2008/cs.pdf

0
So Sir. we are supposed to correct the question paper and apply the logic ?
+1
They happen rarely and this one is most stupid mistake. Everyone can point out it.
+1
yes. And this year they have answer scrutiny. So, if you find any question wrong do not bother to spend time on it- but question wrong is extremely rare so do not count too much on this.
0
if there is any mistake in the question marks are awarded to everyone right
f(c,b,a)
f(c,&c,&(&c))=f(4,4,4)
c is 4, b is a pointer pointing address of a, a is a pointer to pointer of c. Hence both b and c are pointing to same memory address i.e., a.
Hence whatever increment operation happens in f, it happens/ reflects on same value i.e., a.
**ppz+=1;
z=**ppz; //z=5
These steps update it to 5 and stored in z.
*py+=2; //changes c to 7, x is unchanged.
y=*py; //y=7
It updates to 7 and stored in y.
x+=3 //x is incremented by 3.
returns x+y+z=7+7+5=19