Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't C provide struct comparison?

Tags:

c

As most C programmers know, you can't directly compare two structures.

Consider:

void isequal(MY_STRUCT a, MY_STRUCT b)
{
    if (a == b)
    {
        puts("equal");
    }
    else
    {
        puts("not equal");
    }
 }

The a==b comparison will AFAIK throw a compile error on any sensible C compiler, because the C standard doesn't allow for built-in structure comparison. Workarounds using memcmp are of course a bad idea due to alignment, packing, bitfields etc., so we end up writing element by element comparison functions.

On the other hand it DOES allow for structure assignment e.g. a = b is entirely legal. Clearly the compiler can cope with that fairly trivially, so why not comparison?

The only idea I had was that structure assignment is probably fairly close to memcpy(), as the gaps due to alignment etc. don't matter. On the other hand, a comparison might be more complicated. Or is this something I'm missing?

Obviously, I'm aware that doing a simple element by element comparison isn't necessarily enough, e.g. if the structure contains a pointer to a string, but there are circumstances where it would be useful.

like image 661
WillW Avatar asked Aug 24 '11 16:08

WillW


People also ask

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.

Can structs be compared C?

I believe the addresses are different. So the test written would fail. The if statement should be like this if(s1.ch == s2.ch) Since there can be one or more members inside structure, so you need to identify that which member of the structure you want to compare.

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 .

Can we equate two structures in C?

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


2 Answers

As others have mentioned, here's an extract from C: A Reference Manual by Harbison and Steele:

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.

like image 81
Maxim Chetrusca Avatar answered Oct 14 '22 14:10

Maxim Chetrusca


Comparison is unsupported for the same reason memcmp fails.

Due to padding fields the comparison would fail in unpredictable ways which would be unacceptable for most programmers. Assignment changes the invisible padding fields, but these are invisible anyway, so nothing unexpected there.

Obviously, you may ask: so why doesn't it just zero-fill all the padding fields ? Sure that would work but it would also make all programs pay for something they might not need.

EDIT

Oli Charlesworth notes in the comments that you may be asking: "why doesn't the compiler generate code for member-by-member comparison". If that is the case, I must confess: I don't know :-). The compiler would have all the needed information if it would only allow comparing complete types.

like image 25
cnicutar Avatar answered Oct 14 '22 14:10

cnicutar