533 views

What will be the output of the following program?

#include <stdio.h>
void f1(int p1, int *p2, int **p3)
{
p1 = 20;
*p2 = p1;
**p3 = *p2;
p1 = 10;
}
int main()
{
int a = 5, b = 5, *c = &b;
f1(a, &b, &c);
printf("%d %d %d", a, b, *c);
}
1. 5 5 5
2. 5 20 20
3. 10 20 20
4. 20 20 20

a is called by value so any modification in a will be lost.
b is call also call by value but being a pointer, the dereferenced effect is visible in caller function (call by pointer) so modification will be there.
same for c.

final values of a,b,c are 5,20,20.

Why the value of b and c is 20,20?

p1=20

*p2 (is the value of b) =p1 ===> b=20

**p3(is the value of b) = *p2 ===> b=b

note that 'c' is a pointer and *c=b

Option b .