[net.lang.c] how has C bitten you

john@polyof.UUCP ( John Buck ) (09/03/85)

This may have been mentioned before, but for those who have not
had the experience of tracking down this "bug", here it is again:

----------- Code follows here ----------
#define	recip(x)	(1.0/x)
main()
{
	double *p, d;

	d = 7.0;
	p = &d;
	d = recip(*p);
	printf("%f  %f\n", d, *p);
}

---------End of Code ---------

From this, I have learned:
#define	recip(x)	(1.0/(x))

However, there is still no guarding against:
	d = 1.0/*p;

except,
	d = 1.0 / *p;   or   d = 1.0/(*p);
but often, you do not realize your mistake until you decipher the
exceedingly inaccurate messages of all C compilers we have.
Sys5 pcc, Sys5 cc (Ritchie compiler, 11/70),  Berkeley 4.2, lint.

They all say pretty much the same thing:
	syntax error on line N
Where N = number of lines in program + 1  (IE a non-existant line)

or, if you are lucky, the first line after the next comment in your
program.

John Buck
Polytechnic Inst. of NY
Route 110
Farmingdale, NY 11735
(decvax!philabs!ron1!polyof!john or
 decvax!mcnc!rti-sel!polyof!john)