All the problems come from the use of &a
, which is a pointer to "an array of five integers", so that pointer arithmetic (when you think in terms of addresses) gets "scaled" by sizeof(a)
(which might e.g. be 20 if int
are 4 bytes and the compiler needs no padding for alignment purposes -- reasonable hypotheses, though far from certain of course.
So, after
int *ptr = (int *)(&a+1);
int *t = (int *)(&a -1);
ptr
is a pointer to int at the memory address "sizeof(a) more than the address a", and t
similarly for "sizeof(a) less than the address of a". Therefore...:
printf("%d %d %d \n", *(a+1), *(ptr-1), *(t+1));
What should be the result?
Quite possibly a segmentation violation, otherwise 20
followed by two completely arbitrary integer values. Since ptr
and t
are pointers to int
, the address arithmetic scaling for their -1
and +1
does not compensate that done on &a
(the scaling in terms of memory addresses is by sizeof(int)
, not sizeof(a)
!), so ptr-1
and t+1
are pointing to (alleged;-) int
s that are respectively "a few int
s after the end of a
" and "a few int
s before the start of a
".
There's no way to know whether at those arbitrary addresses there is any memory which the process is allowed to address (whence the possibility for segmentation violatons), and, if any accessible memory is there, what its contents "seen as an int
" might possibly be.
Edit: @caf points out that ptr - 1
is not invalid -- it correctly points to the last element of a
; so the output (unless there's a segmentation fault, which @NullUserException thinks is very unlikely but on this point we disagree;-) would start with 20 50
before the third, "arbitrary" junk. Point is, per the C standard, it is valid to compute (though not to use) the pointer "just one past the end" of an array, and the sizeof an array must be exactly that array's length time the sizeof its elements (padding is allowed for an element's type, if needed, and if so it shows in the element's own sizeof, but not for the array as a whole). Subtle, but important;-).