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));
}View worked solution
Answer: B
Let's trace the execution of the program step-by-step:
1. Inside main():
c = 4bpoints toc(i.e.,*bisc)apoints tob(i.e.,**aisc)
When calling f(c, b, a):
xis passed by value, sox = 4initially. Changes toxdo not affectc.pypoints toc(viab). Modifying*pymodifiesc.ppzpoints toc(viaaandb). Modifying**ppzmodifiesc.
2. Inside f():
**ppz += 1;: Since**ppzrefers toc,cbecomes .z = **ppz;:zbecomes .*py += 2;: Since*pyrefers toc,cbecomes .y = *py;:ybecomes .x += 3;: The local variablexbecomes .
3. Return value:
The function returns x + y + z, which is . Therefore, the program prints 19.
Free practice · Your answers on this page are not saved.
