Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are structs not allowed in equality expressions in C? [duplicate]

Tags:

c

standards

c99

c11

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)?

like image 820
stefanct Avatar asked Oct 28 '17 23:10

stefanct


People also ask

Can you set structs equal to each other in C?

Yes, you can assign one instance of a struct to another using a simple assignment statement.

Why we Cannot compare structures in C?

Because the operator == is not aware of all the members of the structure it would need to compare, you are! You can't compare strings either, in C - at least, not with '==' or equivalent.

Why we Cannot compare structure?

We should not compare structures because of the holes between the member fields of a structure. Comparing the structures themselves as variables won`t work. Compare each element of the structure. You should not compare structure variables directly,using *pointer variables comparing two varables is possible .

What operators are applicable on struct data type object in C?

In C, the only operation that can be applied to struct variables is assignment. Any other operation (e.g. equality check) is not allowed on struct variables.


1 Answers

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

like image 64
Kovalainen Avatar answered Oct 17 '22 16:10

Kovalainen