use of NULL

Piet van Oostrum piet at ruuinf
Thu Feb 23 19:44:31 AEST 1989


In article <973 at optilink.UUCP>, cramer at optilink (Clayton Cramer) writes:
 `In article <399 at twwells.uucp:, bill at twwells.uucp (T. William Wells) writes:
 `: In article <965 at optilink.UUCP: cramer at optilink.UUCP (Clayton Cramer) writes:
 `: : . Using 0 instead of NULL is perfectly acceptable.
 `: :
 `: : No it isn't.  Segmented architecture machines will have problems with
 `: : that in large model.  Microsoft defines NULL as 0L, not 0, in large
 `: : model.  Pushing an int 0 instead of a long 0 will screw you royally
 `: : on the PC.
 `: 
And on and on and on and on .....

You are all wrong. ANSI C says that the CONSTANT 0 used in a pointer
context should always give the ``null'' pointer for that pointer type.

BUT

It must be clear from the context that a pointer type is required.

The only good way to give a null pointer to a function is to write

		funct ( (foo *) 0 );
                        ^^^^^^^

i.e cast it. If you have function prototypes, the cast will be done by the
compiler. Even if you want to use NULL you have to write:

		funct ( (foo *) NULL );

If you don't cast, it may work on your particular compiler, but some day it
will hit you, unless you throw away your program before anybody else gets a
chance to use it.
-- 
Piet van Oostrum, Dept of Computer Science, University of Utrecht
Padualaan 14, P.O. Box 80.089, 3508 TB Utrecht, The Netherlands
Telephone: +31-30-531806. piet at cs.ruu.nl (mcvax!hp4nl!ruuinf!piet)



More information about the Comp.lang.c mailing list