In Find size of array without using sizeof, the size of an array is computed via
int arr[100];
printf ("%td", (&arr)[1] - arr);
Now, for the purposes of pointer arithmetic, arr
is considered the element of a single-element array, so
&arr + 1
is a pointer one-past the end of that (conceptual) single-element array, so when the address of arr[0]
is subtracted, the number of elements in arr
is obtained.
In (&arr)[1]
, that pointer is the operand of the indirection operator,
(&arr)[1] ≡ *(&arr + 1)
and the resulting array expression is then converted to an int*
as per 6.3.2.1 (3).
So far, so good. But the last sentence in 6.5.6 (8) (Additive operators),
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.
forbids the evaluation of the indirection operator there.
The question is whether the indirection operator is evaluated in
*(&arr + 1) - arr
(in which case that expression would invoke undefined behaviour) or the array-to-pointer conversion annihilates the evaluation (in which case all is well), like taking the address (&(*(&arr + 1))
) or applying sizeof
to it would..