Article View: comp.std.c
Article #323struct comparison
From: Maarten Litmaath
Date: Thu, 13 Jul 1989 22:34
Date: Thu, 13 Jul 1989 22:34
35 lines
1481 bytes
1481 bytes
According to the pANS structs can be
- assigned
- returned
- used as function arguments
but they cannot be COMPARED, right? Very annoying.
The reason appears to be internal padding, whose contents aren't specified.
Thus the following won't work:
struct foo {
char bar;
int baz;
double zork;
} mork, mindy;
...
puts(bcmp((char *) &mork, (char *) &mindy, sizeof mork) == 0 ?
"equal" : "unequal");
To compare the 2 structs, one now has to check each field oneself:
puts(mork.bar == mindy.bar && mork.baz == mindy.baz
&& mork.zork == mindy.zork ? "equal" : "unequal");
Why does the PROGRAMMER have to go through all that trouble?
I just want to say:
puts(mork == mindy ? "equal" : "unequal");
Is the ANSI committee trying to tell us the compiler cannot transform the
equality test into the correct member-by-member comparison code?
--
"... a lap-top Cray-2 with builtin |Maarten Litmaath @ VU Amsterdam:
cold fusion power supply" (Colin Dente) |maart@cs.vu.nl, mcvax!botter!maart
Technical Details
Message-ID:
<2874@solo3.cs.vu.nl>Path:
arc1-ca.pugleaf.net!utzoo!utgpu!jarvis.csri.toronto.edu!rutgers!tut.cis.ohio-state.edu!gem.mps.ohio-state.edu!ginosko!uunet!mcvax!hp4nl!botter!star.cs.vu.nl!maartShow Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!utgpu!jarvis.csri.toronto.edu!rutgers!tut.cis.ohio-state.edu!gem.mps.ohio-state.edu!ginosko!uunet!mcvax!hp4nl!botter!star.cs.vu.nl!maart From: maart@cs.vu.nl (Maarten Litmaath) Newsgroups: comp.std.c Subject: struct comparison Keywords: padding, bcmp, fields, compiler's job Message-ID: <2874@solo3.cs.vu.nl> Date: 13 Jul 89 22:34:39 GMT Organization: V.U. Informatica, Amsterdam, the Netherlands Lines: 35