C中的结构不能为空,因为语法禁止它。此外,还有一个语义约束,如果一个结构体没有命名的成员,行为是不确定的:
struct-or-union-specifier:
struct-or-union identifieropt { struct-declaration-list }
struct-or-union identifier
struct-or-union:
struct
union
struct-declaration-list:
struct-declaration
struct-declaration-list struct-declaration
struct-declaration:
specifier-qualifier-list struct-declarator-list ;
/* type-specifier or qualifier required here! */
specifier-qualifier-list:
type-specifier specifier-qualifier-listopt
type-qualifier specifier-qualifier-listopt
struct-declarator-list:
struct-declarator
struct-declarator-list , struct-declarator
struct-declarator:
declarator
declaratoropt : constant-expression
如果你写
struct identifier { };
它会给你一个诊断信息,因为你违反句法规则。如果你写
struct identifier { int : 0; };
那么你有一个没有命名成员的非空结构,因此使行为不被定义,而不需要诊断:
If the struct-declaration-list contains no named members, the behavior is undefined.
请注意,不允许以下内容,因为灵活的数组成员不能是第一个成员:
struct identifier { type ident[]; };