a[0] a[1] a[2] a[3] a[4] // array indexes
3 1 2 -2 -4 // value
1001 1005 1009 1013 1017 // Consider it is memory location.
p == 1001; // p=a;
Incrementation will done like this. p+ (value * sizeof(int) )
In first loop,
1001 + 3 ; // *(p + *p); p== 1001 , *p == 3
So it will print the value placed in the 1013 that is equal to -2.
After that you are doing this `p += *p;` Now `p` points to `1013`
Second loop,
1013 + -2; // *(p + *p); p== 1013 , *p == -2
So it will print 1 (1005 )
. After that assigning , now p
points to 1005
.
Third loop,
1005 + 1 ; // *(p + *p ); p == 1005 , *p == 1
It will print 2(1009)
. Now p
changes into 1009
.
Fourth loop,
1009 + 2 ; // *(p + *p ); p == 1003 , *p == 2
It will print -4(1017)
and p changes into 1017
.
At finally,
1017 + -4 ; // It becomes 1001.
So It will print the value in 1001. so the value is 3.