#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 whichr
points to gets changed top
. - Now
r
points toq
that has address ofc
, so nowq
has address ofa
becausep
points toa
. Soq
now points toa
. So*q
gives 6. - Since
r
still points toq
, andq
points toa
,**r
gives 6. - So
*q + **r = 6+6=12
Is this the correct explanation?