C11 6.2.7 p3:
If both types are array types, the following rules are applied:
If one type is an array of known constant size, the composite type is an array of that size.
Otherwise, if one type is a variable length array whose size is specified by an expression that is not evaluated, the behavior is undefined.
Otherwise, if one type is a variable length array whose size is specified, the composite type is a variable length array of that size.
Otherwise, if one type is a variable length array of unspecified size, the composite type is a variable length array of unspecified size.
Otherwise, both types are arrays of unknown size and the composite type is an array of unknown size.
For this paragraph, I have the following several questions:
Could you give an example of the application of "Otherwise, if one type is a variable length array whose size is specified by an expression that is not evaluated, the behavior is undefined"?
If there are two variable length array types with specified sizes, then which size of the variable length array type is the composite type? Just like the following code:
int n=2;
int m=3;
void f(char(*)[n]);
void f(char(*)[m]);
What is the composite type of the function f?
Thank you for your reading.
1.Could you give an example of the application of "Otherwise, if one type is a variable length array whose size is specified by an expression that is not evaluated, the behavior is undefined"?
The result type of the conditional operator A ? B : C
, is determined by the types of the second and third operands. If they are both pointers to compatible types, “the result type is a pointer to an appropriately qualified version of the composite type” (C 2018 6.5.15 6).
Consider 1 ? (char (*)[]) 0 : (char (*)[foo()]) 0
. The second operand type is pointer to an array of an unspecified number of char
. The third operand is a pointer to a variable length array. However, since the first operand is 1, the third operand is not evaluated. So the result type would be a composite type involving a type that is a variable length array whose size is specified by an expression that is not evaluated.
(While this is demonstrated here by converting zero to a pointer type, the situation arises in real code from time to time and is the subject of an open Clang bug.)
2.If there are two variable length array types with specified sizes, then which size of the variable length array type is the composite type?
I do not see this specified in the C 2018 standard. I think there may be no situation where a composite type is formed from two variable length array types whose sizes are both evaluated.
For example, with the conditional operator, at most one operand is evaluated. For a declaration of a function, size expressions are evaluated only upon entry to the function, so the size expressions in the function definition are used, and size expressions in any non-definition declarations are not used.