Header problems

Daniel A. Glasser dag at chinet.UUCP
Wed Mar 9 01:55:27 AEST 1988


In article <7412 at brl-smoke.ARPA> gwyn at brl.arpa (Doug Gwyn (VLD/VMB) <gwyn>) writes:
>NULL is defined in <stdio.h> (for historical compatibility) and in
><stddef.h> (where it really belongs).  If you need to use the symbol,
>you should include one or both of these two headers.  Of course NULL
>is only a "convenience" macro, because it is clear how to write any
>type of null pointer constant in the language proper.  It was included
>in the propsoed standard because so much code uses it, not because it
>is technically necessary for portability.
>
>The implementor should put something like the following in each header:
>	#define NULL 0
>
>
>P.S. These are my view, not necessarily X3J11's.


Your use of NULL === 0 promotes unportable code, and is considered to be
bad programming style in a world where portability across multiple machine
architectures (ie, PDP-11, VAX-11, M680x0, I80x86 and Z800x) is required
for commercial reasons.

On machines were sizeof int != sizeof(void *), the above definition will
not work on older style function calls (without prototypes) or in var-arg
situations.  Requiring sizeof int == sizeof(void *) is not a viable
solution.  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.

What the C language guarentees is not that 0===NULL, but that a constant 0
can be assigned/compared to a pointer without warning or error regardless
of pointer representation and compare as equal to the NULL pointer.
In cases where the compiler is unable to determine if an int or type *
is being passed, 0 is passed as an int.  The use of the name NULL in the
preprocessor does not affect this.

('===', as used above, means exactly equivilant)
-- 
			   Daniel A. Glasser
...!ihnp4!chinet!dag || ...!ihnp4!mwc!dag || ...!ihnp4!mwc!gorgon!dag
    One of those things that goes "BUMP!!! (ouch!)" in the night.



More information about the Comp.lang.c mailing list