This expression invokes undefined behavior as a result of the dereference operator *
:
int *ptr = *( ( &a ) + 1 );
First, let's start with ( &a ) + 1
. This part is valid. &a
has type int (*)[5]
, i.e. a pointer to an array of size 5. Performing pointer arithmetic by adding 1 is valid, even though a
is not an element of an array.
In section 6.5.6 of the C standard detailing Additive Operators, paragraph 7 states:
For the purposes of these operators, a pointer to an object that is
not an element of an array behaves the same as a pointer to the first
element of an array of length one with the type of the object as its
element type.
It's also allowed to create a pointer that points to one element past the end of an array. So &a + 1
is allowed.
The problem is when we dereference this expression. Paragraph 8 states:
When an expression that has integer type is added to or subtracted
from a pointer, the result has the type of the pointer operand. If the
pointer operand points to an element of an array object, and the array
is large enough, the result points to an element offset from the
original element such that the difference of the subscripts of the
resulting and original array elements equals the integer expression.
In other words, if the expression P points to the i-th element of an
array object, the expressions (P)+N (equivalently, N+(P)) and (P)-N
(where N has the value n) point to, respectively, the i+n-th and
i−n-th elements of the array object, provided they exist. Moreover, if
the expression P points to the last element of an array object, the
expression (P)+1 points one past the last element of the array object,
and if the expression q points one past the last element of an array
object, the expression (Q)-1 points to the last element of the array
object. If both the pointer operand and the result point to elements
of the same array object, or one past the last element of the array
object, the evaluation shall not produce an overflow; otherwise, the
behavior is undefined. If the result points one past the last element
of the array object, it shall not be used as the operand of a unary *
operator that is evaluated.
Since dereferencing a pointer to one past the end of an array is not allowed, the beahvior is undefined.
Going back to the expression in the referenced post:
int *ptr = (int*)(&a+1);
printf("%d %d", *(a+1), *(ptr-1));
This is also undefined behavior but for a different reason. In this case, a int (*)[5]
is converted to int *
and the converted value is subsequently used. The only case where using such a converted value is legal is when converting an object pointer to a pointer to a character type, e.g. char *
or unsigned char *
and subsequently dereferenced to read the bytes of the object's representation.
EDIT:
It seems the two lines above are actually well defined. At the time the pointer dereference *(ptr-1)
occurs, the object being accessed has effective type int
, which matches the dereferenced type of ptr-1
. Casting the pointer value &a+1
from int (*)[5]
to int *
is valid, and performing pointer arithmetic on the casted pointer value is also valid because it points either inside of a
or one element past it.