First up, if you want truly portable code, you shouldn't be attempting to create an array of size zero1, as you did in your original question, now fixed. But, since it's not really relevant to your question of whether sizeof(p->a)
is valid when p == NULL
, we can ignore it for now.
From C11 section 6.5.3.4 The sizeof and _Alignof operators
(my bold):
2/ The sizeof
operator yields the size (in bytes) of its operand, which may be an expression or the parenthesized name of a type. The size is determined from the type of the operand. The result is an integer. If the type of the operand is a variable length array type, the operand is evaluated; otherwise, the operand is not evaluated and the result is an integer constant.
Therefore no evaluation of the operand is done unless it's a variable length array (which your example is not). Only the type itself is used to figure out the size.
1 For the language lawyers out there, C11 states in 6.7.6.2 Array declarators
(my bold):
1/ In addition to optional type qualifiers and the keyword static
, the [
and ]
may delimit an expression or *
. If they delimit an expression (which specifies the size of an array), the expression shall have an integer type. If the expression is a constant expression, it shall have a value greater than zero.
However, since that's in the constraints section (where shall
and shall not
do not involve undefined behaviour), it simply means the program itself is not strictly conforming. It's still covered by the standard itself.