The value printed by the following program is _______.
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);
}View worked solution
Answer: 30
Let us trace the execution of the program step-by-step:
- In
main(), the variables are initialized as:i = 5,j = 10 - The function call
f(&i, j)is made:- The pointer
preceives the address ofi(pass-by-reference), so*prefers directly toi. - The variable
mreceives a copy of the value ofj(pass-by-value), som = 10.
- The pointer
- Inside the function
f():m = m + 5.*p = *p + m. Sinceppoints toi, the value ofibecomes .
- After returning to
main():- The value of
iis updated to . - The value of
jremains unchanged at because it was passed by value.
- The value of
- Finally,
printf("%d", i+j)prints the value of .
Free practice · Your answers on this page are not saved.
