Useful macro...or dangerous??

Brian Matthews blm at cxsea.UUCP
Tue May 3 01:38:20 AEST 1988


Chris Torek (chris at mimsy.UUCP) writes:
|[Dangerous]
|In article <221 at raunvis.UUCP> kjartan at raunvis.UUCP (Kjartan Pier
|Emilsson Jardedlisfraedi) writes:
|For instance, if I write
|
|	struct holey {
|		char	ch;
|		int	i;
|	} smokes, batman;
|
|there is often a `gap' between `ch' and `i'.

Another problem that may occur is if you have something like:

	struct maybe_holey {
		char	arr[16];
	} string, array;

then do something like:

	strcpy (string.arr, "kiwi fruit");
	strcpy (string.arr, "bananas");
	...
	strcpy (array,arr, "mangos");
	strcpy (string.arr, "mangos");

equal will now report that array and string aren't equal, even though they
should be considered equal.  The reason is of course that the characters
beyond the end of the string aren't significant in this case, but equal
doesn't know this.  This is one of the reasons that structure equality
can't be done reasonably in the compiler.  In general, the compiler will
know about the gaps that Chris mentions, but it won't know whether
string.arr is being used as an array, where all characters are
significant, or a string, where only the characters up to and including
the null are significant.  And of course equal can't know either of
these.

|In short, it is safest not to do this.

Agreed.

-- 
Brian L. Matthews                               "The first time I died,
...{mnetor,uw-beaver!ssc-vax}!cxsea!blm          was in the arms of good
+1 206 251 6811                                  friends of mine."
Computer X Inc. - a division of Motorola New Enterprises



More information about the Comp.lang.c mailing list