struct comparison

Maarten Litmaath maart at cs.vu.nl
Fri Jul 14 08:34:39 AEST 1989


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 at cs.vu.nl, mcvax!botter!maart



More information about the Comp.std.c mailing list