NULL etc.

Doug Gwyn gwyn at brl-smoke.ARPA
Fri Jul 1 02:39:26 AEST 1988


In article <6966 at cup.portal.com> Paul_L_Schauble at cup.portal.com writes:
>  is #define NULL (char *)0 really portable??

No.  It never has been, and in Standard C it will cause diagnostics.

>This becomes a very interesting question since I have seen postings that
>state that the value on NULL for ANSI C is (void *)0. Seems to me that this
>has all of the same problems. Is this really what the standard says? If so,
>where have I gone wrong above?

Where you have gone wrong is in not reading carefully (or not being
sufficiently particular about whom you believe).

Standard-conforming implementations shall define NULL (in the headers
where it is defined) as either 0 or ((void *)0), implementor's choice.
The latter definition can be used to catch more programming errors (for
example, using NULL as a case in a switch or as an array index).

void *s have special properties (not surprising, since there are no void
objects).  The relevant property here is that any valid pointer may be
converted to void * and back without loss of significant information.
This, combined with the requirement that null pointers of all types
compare equal, implies that ((void *)0) is just as good as 0 for use as a
"generic" null pointer.  ("Generic" is in quotes because NULL isn't really
generic, it just acts that way in most contexts.)



More information about the Comp.lang.c mailing list