Pointers program

The four pointer operations in the printf:

*(*(p+i)+j)
*(*(j+p)+i)
*(*(i+p)+j)
*(*(p+j)+i)

Evaluate to the following:

*(*(p+i)+j) -> *(p[i]+j) 
*(*(j+p)+i) -> *(p[j]+i)
*(*(i+p)+j) -> *(i[p]+j) 
*(*(p+j)+i) -> *(j[p]+i)

p[n] is the same as n[p] as the pointer logic (as seen above) is commutitive around +.
see this question for more details.

So there are really only two statements:

*(p[i]+j) 
*(p[j]+i)

Of course, this is just p + offset [x] + offset. So really, it is only one statement:

*p[i+j]

Which is of course, the value stored in the p array at offset i+j.

Because of the nested loops, the values of i and j are as follows:

i j  i+j
0,0   0
0,1   1
1,0   1
1,1   2

So it prints in turn, the values in each location of p (1,2, 2and3) four times.

Leave a Comment