_Generic and VLA type compatibilityAuthors: Jay Ghiron
Date: 2026-06-16
Submitted against: C23
Status: Open
According to N3348 the following is undefined:
int main(){
int n=2;
int(*v)[n];
_Generic(v,int(*)[3]:1);
}
Presumably, because of the following:
If the two array types are used in a context which requires them to be compatible, it is undefined behavior if the two size specifiers evaluate to unequal values.
(C23 6.7.7.3 "Array declarators" paragraph 6.)
In this context if they were not compatible the program would be invalid, so it appears that it is undefined. However, if a default generic association was added with the exact same expression as the other generic association:
int main(){
int n=2;
int(*v)[n];
_Generic(v,int(*)[3]:1,default:1);
}
Then even if the types were not compatible, the result would be exactly the same. Therefore it appears to be defined, since the types being compatible has no effect. If the expressions of the generic associations are different and both valid in the surrounding context is it less clear whether or not it is undefined, since type compatibility does not affect the validity of the generic selection but still does affect the result.
Was it intended for the validity (in terms of the behavior being defined) of the previous two programs to be different, or should they both be the same? Note that the following is defined, because the controlling expression is unevaluated:
int main(){
int n=2;
_Generic((int(*)[n])0,int(*)[3]:1);
}