Header problems

Pablo Halpern pablo at polygen.uucp
Thu Mar 10 09:59:49 AEST 1988


>From article <3351 at chinet.UUCP>, by dag at chinet.UUCP (Daniel A. Glasser):
>            A better way to do it, in each place you want to define NULL,
> is:
> 
> 	#ifndef	NULL
> 	#define	NULL	((void *)0)
> 	#endif
> 
> On older compilers, replace 'void' with 'char'.  This allows the use of
> a short NULL representation when the compiler is smart enough to use it
> but forces passing of NULL in argument lists as a pointer sized object.

Do NOT replace 'void' with 'char' for old compilers unless you are prepared
to cast every use of NULL to the pointer type you are assigning (or comparing,
etc.).  Most compilers will complain of a pointer type missmatch since
(char *) doesn't match (int *) or (struct foo *) etc..  I agree that for
ANSI compilers, the above is correct, since (void *) DOES match (int *)
by definition.  For compilers that don't support (void *), you must have
a compiler-specific definition of NULL:

	#define NULL 0		/* If sizeof(int) == sizeof(char *) */

or

	#define NULL 0L		/* if sizeof(long) == sizeof(char *) */

This avoids pointer type missmatches because all compilers let you compare
any pointer agains integer zero.  Since the above #defines
are necessarily machine (and compiler) specific, I put these
types of declarations in a file called "machine.h" which contains machine
and compiler-dependent declarations and is different for each compiler I
use.  "machine.h" is #include'd in all of my source files.

Pablo Halpern		mit-eddie \
Polygen Corp.		princeton  \ polygen!pablo
200 Fifth Ave.		bu-cs	   /
Waltham, MA  02254	stellar   /



More information about the Comp.lang.c mailing list