NULL pointers

Chris Torek chris at mimsy.UUCP
Sat Nov 22 03:02:46 AEST 1986


In article <1696 at ncoast.UUCP> kent at ncoast.UUCP (Kent Williams) writes:
>... the use of NULL is a constant source of pain for which
>it seems there is a simple solution to wit
>
>#ifdef HASVOID
>#define NULL ((void *)0)
>#else
>#define NULL 0L
>#endif

This is neither necessary nor sufficient:

>If NULL is typed to have the size of the largest possible object that it
>will be assigned to, it will be 'narrowed' to fit into whatever object you
>are assigning it to.

If you are using `0' (the present proper definition for NULL) in
an assignment or comparison context, the narrowing or widening or
implicit cast conversion or whatever-is-required is done automatically.
If you are using 0 in some other context, the information as to
just what narrowing or widening or implicit cast conversion or
whatever-is-required is unavailable.  It is not difficult to add
an assignment context by using an explicit cast:

	foo((char *) NULL);	 /* or foo((char *) 0); */

In C++, or some other C-like language with function prototypes, the
prototype can provide the context:

	extern void foo(char *s);
	...
	foo(NULL);		/* or foo(0); */

In either case, an explicit cast is still legal, and may help the
reader as well as the compiler.
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7690)
UUCP:	seismo!mimsy!chris	ARPA/CSNet:	chris at mimsy.umd.edu



More information about the Comp.lang.c mailing list