The unavailability of structs as comparison operands is one of the more obvious things in C that don't make too much sense (to me). structs can be passed by value and copied via assignments but ==
is not specified for them.
Below are the relevant parts of the C11 standard (draft) that define the constraints of the equality operators (==
and !=
) and the simple assignment operator (=
). Note the lack of structures and unions in the constraints of equality operators. (Apart from the lack of dealing with _Atomic
the wording in C99 is the same).
6.5.9 Equality operators
Constraints
One of the following shall hold:
- both operands have arithmetic type;
- both operands are pointers to qualified or unqualified versions of compatible types;
- one operand is a pointer to an object type and the other is a pointer to a qualified or unqualified version of void; or
- one operand is a pointer and the other is a null pointer constant.
6.5.16.1 Simple assignment
Constraints
One of the following shall hold:
- the left operand has atomic, qualified, or unqualified arithmetic type, and the right has arithmetic type;
- the left operand has an atomic, qualified, or unqualified version of a structure or union type compatible with the type of the right;
- the left operand has atomic, qualified, or unqualified pointer type, and (considering the type the left operand would have after lvalue conversion) both operands are pointers to qualified or unqualified versions of compatible types, and the type pointed to by the left has all the qualifiers of the type pointed to by the right;
- the left operand has atomic, qualified, or unqualified pointer type, and (considering the type the left operand would have after lvalue conversion) one operand is a pointer to an object type, and the other is a pointer to a qualified or unqualified version of void, and the type pointed to by the left has all the qualifiers of the type pointed to by the right;
- the left operand is an atomic, qualified, or unqualified pointer, and the right is a null pointer constant; or
- the left operand has type atomic, qualified, or unqualified _Bool, and the right is a pointer.
Can anybody explain why this difference exists (without speculating)?
Structures and unions cannot be compared for equality, even though assignment for these types is allowed. The gaps in structures and unions caused by alignment restrictions could contain arbitrary values, and compensating for this would impose an unacceptable overhead on the equality comparison or on all operations that modified structure and union types.
From "C: A Reference Manual". Even memcmp
can fail when comparing structs, for the same reason (the compiler adding additional buffer space for alignment purposes). I guess they could implement member-by-member comparison; why they haven't is a different issue
来源:https://stackoverflow.com/questions/46995631/why-are-structs-not-allowed-in-equality-expressions-in-c