Probably, there is a contradiction is the C standard for VM types used in conditional operator. Assume:
int f(void) { return 42; }
Now, what is the type of a following expression?
1 ? 0 : (int(*)[f()]) 0
It's value must be equal to NULL but I am not sure what is the type.
The int(*)[f()]
is a pointer to VLA of size f()
. However, to complete this VM type the size expression f()
must be evaluated. The problem is that it belong to a branch of ternary operator which is not evaluated. From 6.5.15p4:
The second operand is evaluated only if the first compares unequal to 0; the third operand is evaluated only if the first compares equal to 0
Rules of the conditional operator require the combination of 0
pointer be the type of the other branch 6.5.15p6:
... if one operand is a null pointer constant, the result has the type of the other operand; ...
How to solve this contradiction? Possible solutions are:
int(*)[f()]
- thef()
is evaluated anywayint(*)[]
- the array type stays incomplete- undefined behavior
- something else?
The rules of composite type suggest that this may be UB but I am not sure if those rules apply for this case. I look for an answer that cites the specification of C17 but wording from upcoming C2X is fine as well.