The expression (&a[0]+1)
gives &a[1]
Yes.
and the output should be 2
No.
but the output of above code is &a[1]
why?
You're asking about the effect of
printf("%u ",*(&a[0]+1));
. We've established that (&a[0]+1)
is equivalent to &a[1]
. It follows that *(&a[0]+1)
is equivalent to *(&a[1])
, which in turn is equivalent to just a[1]
. The value of a[1]
is an array, not 2
, notwithstanding the fact that 2
is the value of the first array element.
Your context is not among the very few in which array values fail to decay to pointers, so the overall function call is in fact equivalent to
printf("%u ", &a[1][0]);
At this point we hit a snag, however. The %u
directive must correspond to an argument of (promoted) type unsigned int
, and the actual argument instead has pointer type. This mismatch results in undefined behavior. It is plausible, but by no means guaranteed, that that manifests for you as printing a decimal integer corresponding to part or all of the pointer's bit pattern.
If you want to print the zeroth element of a[1]
, then it's easiest to write that simply as a[1][0]
, but you may also write it as *a[1]
or **&a[1]
or **(&a[0] + 1)
or in many other forms.