#include <stdio.h>
int main(void){
int a=1,b=2,c=3; int *p,*q,**r; p=&a;
r=&q;
q=&c;
a=*q+**r;
printf("x=%d y=%d z=%d\n",**r,*p,*q);
*r=p;
a=*q+**r;
printf("w=%d\n",a);
return 0;
}
Output:
x=3 y=6 z=3
w=12
I was able to predict the output correctly, but I am not sure whether I have the correct explanation for the output of z.
Please see whether I have the correct understanding:
- Right before
*r=p;executes, we havea=6,b=2,c=3. - When
*r=p;executes, the value at the place to whichrpoints to gets changed top. - Now
rpoints toqthat has address ofc, so nowqhas address ofabecauseppoints toa. Soqnow points toa. So*qgives 6. - Since
rstill points toq, andqpoints toa,**rgives 6. - So
*q + **r = 6+6=12
Is this the correct explanation?