how has C bitten you?

Peter S. Shenkin peters at cubsvax.UUCP
Mon Aug 26 23:24:49 AEST 1985


I've had several bugs involving code hidden in macro definitions which have 
been very difficult to find.  One I recall offhand went something like this:

/* OPEN MOUTH *****************************************************************/
#define Coords(I)	(complicated.structure.redirection[I].x, \
			 complicated.structure.redirection[I].y, \
			 complicated.structure.redirection[I].z   )
main()
{
	...
	subr(Coords(i));  /* BITE */
	...
}
/***************************************************************************/
subr(x,y,z)
float x,y,z;
{...}
/* SWALLOW ******************************************************************/

Problem is, when expanded, the call to subr looks like
	subr((exp1,exp2,exp3));
The comma operator is applied, and subr() gets only exp1 !!!  The interesting
thing is that if anyone had asked me, whether (something), ((something)),
and (((something))) mean the same in C, I would have said "Yes," without
thinking.  Obviously, I would have been wrong.

Peter S. Shenkin	philabs!cubsvax!peters		Columbia Univ. Biology



More information about the Comp.unix.wizards mailing list