C Records: Value Equality Classes for Identity
The calculator for this page is switched off while it is checked. The formula is below, and the worked example in the article shows how to use it by hand.
This article explains how C records (structs) can be compared for value equality, and why identity-based comparison (pointer equality) is often the default in C. You will learn the difference between value equality and identity, how to implement value equality for structs, and when each approach is appropriate.
Formula
For simple numeric values, equality is defined by the == operator. For structs, there is no built-in operator; you must compare each member. The formula for value equality of two structs s1 and s2 is:
s1.member1 == s2.member1 && s1.member2 == s2.member2 && ...
Each variable is a member of the struct, and the comparison is done member-by-member. If all members are equal, the structs are value-equal.
Worked Example
Using the default values from the calculator (10 and 20), the equality check is 10 == 20, which is false (0). If you change both values to 15, the result is true (1). For a struct, imagine struct Point { int x; int y; }. Compare p1.x == p2.x && p1.y == p2.y.
Value Equality vs. Identity in C
In C, every object has an identity: its address in memory. Two pointers are equal if they point to the same object. This is identity comparison. Value equality, on the other hand, compares the contents of two objects. For primitive types (int, char, float), the == operator does value comparison. For structs, you must write a function that compares each field.
Why does C not have built-in struct equality? Because structs can contain padding bytes, pointers, and nested structures, making a simple bitwise comparison unreliable. The language leaves it to the programmer to define what “equal” means for each type.
How to Implement Value Equality for Structs
To compare two structs by value, write a function that returns true if all members are equal. For example:
int point_equal(struct Point a, struct Point b) {
return a.x == b.x && a.y == b.y;
}
For structs containing arrays, use memcmp or a loop. For strings, use strcmp. For pointers, decide whether you want to compare the addresses or the pointed-to values.
- If the struct has only numeric members, compare each with
==. - If it has arrays, compare element-by-element or use
memcmp. - If it has pointers, compare the pointed-to values (deep equality) or the pointers themselves (shallow equality).
Choosing Between Value Equality and Identity
Which one you choose depends on the use case. Use identity when you want to know if two variables refer to the same object (e.g., in a linked list, checking if a node is the same). Use value equality when you want to know if two objects have the same data (e.g., comparing two coordinates).
In many data structures, like hash tables, you need value equality to find a stored item. In others, like a graph, you might use identity to avoid cycles.
Common Problems and Pitfalls
One common mistake is using == on structs directly, which is a compile error in C. Another is comparing floating-point members with ==, which can fail due to rounding. Also, forgetting to compare all members leads to false positives.
Padding bytes can cause memcmp to fail even when values are logically equal, because uninitialized padding may differ. Always initialize your structs (e.g., with memset) to avoid this.
Alternatives and Best Practices
In C, you can use memcmp for a quick bitwise comparison, but only if the struct has no padding and no floating-point members. For portable code, write a manual comparison function. In C++, you can overload operator==. In other languages, like Java or C#, value equality is often built-in (e.g., equals()).
Best practice: always document whether your comparison is shallow or deep, and be explicit about how pointers and arrays are handled.
Frequently Asked Questions
Can I use == on structs in C?
No, C does not support the == operator for structs. You must compare each member manually or use memcmp (with caution).
What is the difference between shallow and deep equality?
Shallow equality compares the immediate members. If a member is a pointer, it compares the address, not the pointed-to value. Deep equality follows pointers and compares the objects they point to.
How do I compare structs containing floats?
Use an epsilon tolerance: fabs(a.f - b.f) < 0.0001. Direct == can fail due to floating-point precision.
Why does memcmp sometimes fail for equal structs?
Because of padding bytes. The compiler may insert unused bytes between members, and those bytes can have arbitrary values. memcmp compares them, causing false inequality.
Should I use value equality or identity for a hash table?
Use value equality, because you want to find an item with the same data, not the same memory address. Hash tables typically require a hash function and an equality function.