Authors: Joseph Myers
Date: 2025-03-05
Submitted against: C23
Status: Open
The definition of compatible type for structure types (C23 6.2.7) allows two structures to be compatible where one ends with a flexible array member and the other ends with a member with complete array type. (Before C23, this was an issue for structures in separate translation units; C23 makes it applicable within a single translation unit.)
struct s { int a; char b[]; } x;
void f()
{
struct s { int a; char b[2]; } y = {};
x = y; // OK, assignment between compatible types.
}
However, the definition of flexible array members explicitly anticipates that the offset of the flexible array member may be different from that of a member with complete array type ("the offset of the array shall remain that of the flexible array member, even if this would differ from that of the replacement array"), which is not consistent with the types being compatible. Furthermore, the rules for assignment (and likewise initialization and argument passing and function return) and conditional expressions are underspecified in this case; it is not clear what members would be copied when such a mixture of types is involved.
In C23 6.2.7 (Compatible type and composite type), after the first bullet point for compatibility for structure and union types, insert another bullet point:
- if one member of the pair is declared with a complete array type, the other is declared with a complete array type;