#include <stdio.h>
void foo(int *p, int x) {
*p=x;
}
int main(){
int *z;
int a = 20, b = 25;
z = &a;
foo(z,b);
printf("%d",a);
return 0;
}The output of the given program is __________. (Answer in integer)
View worked solution
Answer: 25:25
Let's trace the execution of the program step-by-step:
- Variable Initialization:
ais initialized to20.bis initialized to25.zis a pointer to an integer, andz = &a;makeszpoint to the memory location ofa.
- Function Call:
- The function call
foo(z, b);is executed. - This passes the pointer
z(which is&a) and the value ofb(which is25) to the functionfoo.
- The function call
- Inside
foo(int *p, int x):- The parameter
preceives the address ofa(i.e.,p = &a). - The parameter
xreceives the value25. - The statement
*p = x;dereferencespand assigns the value ofxto the memory location it points to. Sinceppoints toa, this updates the value ofato25.
- The parameter
- Printing the Output:
- Back in
main(),printf("%d", a);prints the current value ofa, which is now25.
- Back in
Therefore, the output of the program is 25.
Free practice · Your answers on this page are not saved.
