NULL pointer
Niket K. Patwardhan
bilbo.niket at locus.ucla.edu
Tue Nov 19 07:49:23 AEST 1985
>Date: Sun, 17 Nov 85 3:59:50 EST
>From: Doug Gwyn (VLD/VMB) <gwyn at BRL.ARPA>
>To: Davidsen <davidsen%steinmetz.uucp at BRL.ARPA>
>cc: info-c at brl-tgr.arpa
>Subject: Re: NULL pointer
>
>I'll see if I can beat Guy Harris to this one:
>
>The whole topic of the correct #definition for NULL
>has been beat to death more than once on this mailing list.
>
>The answer is that NULL must be defined as simply 0
>and that code that misuses NULL cannot be fixed in
>general by any change in its #definition.
>
>Consider
>
> hi_func()
> {
> ...
> lo_func( ..., NULL, ... );
> ...
> }
>
> lo_func( ..., p, ... )
> int *p;
> {
> ...
> }
>
>This code is INCORRECT, whether NULL is #defined as 0
>or as (char *)0. The only correct way to do this
>(not counting the new X3J11 parameter type coercion
>mechanism) is to change the invocation of lo_func to:
>
> lo_func( ..., (int *)NULL, ... );
>
>By trying to #define NULL as (char *)0, you are trying
>to keep the programmer from having to understand this
>and deal with it properly. You also cause problems
>with statements such as
>
> if ( p == NULL )
> do_stuff;
>
>when the type of `p' is not (char *). The only
>universal #definition for NULL that works in all such
>cases is 0. (Of course, some machines allow sloppier
>usage. This discussion assumes universality.)
>
>#define NULL 0 /* don't muck around with it */
The last line: ---- I agree with it whole-heartedly.
But..... for a machine with non-zero null pointer representations
> lo_func( ..., (int *)NULL, ... );
still doesn't work. What the callee routine gets is a pointer with an all-zero
representation.... which isn't a null pointer. The ONLY thing that does work is
lo_func( ..., p=0, ....);
where p is a pointer of the requisite type! Of course, with parameter type
coercion (or should I say conversion.. it isn't the same thing), this
shouldn't be necessary!
More information about the Comp.lang.c
mailing list