Null pointers and constant zero
Joe Beckenbach
beckenba at cit-vax.Caltech.Edu
Thu Jul 7 11:53:42 AEST 1988
In article <402 at proxftl.UUCP> bill at proxftl.UUCP (T. William Wells) writes:
>There is a very common confusion about zero and null pointers,
>that they are the same thing. This has been reenforced by the
>fact that on many machines this assumption will always work.
`0' cast to pointers makes a null pointer of that type.
This means that NULL === (cast) 0. Standard practice is to say
NULL === 0, then cast the typeless. [Or just ignore it, since the
compiler is `supposed to cast things correctly anyway' :-| ]
This echos Mr. Wells' article.
>Note that this does not fix the problem with functions; calling
>func(NULL) is as wrong as calling func(0). If NULL is defined as
>0, they are equivalent. If NULL is defined as ((void *)0), there
>is nothing that guarantees that the ((void *)0) has the same
>representation as bar *.
>
>A personal note: I do not use NULL at all, because I do not
>think the confusion created by its use is worth the minimal or
>nonexistent improvement in program clarity.
Given all the above, I have decided long since to let the
compiler do all the worrying about the actual bits. Another poster
has also pointed out that THE null pointer does not exist, simply
a null pointer for each type. My encapsulization of the whole mess:
( previously given #define NULL 0 [or whatever] )
#define NIL(type) (type) NULL
and never-after do I even think of NULLs, only NIL pointers of the
appropriate type. This dramatically improves clarity to my eye, and
solves the typeless-constant problem to boot.
--
Joe Beckenbach beckenba at csvax.caltech.edu Caltech 1-58, Pasadena CA 91125
Mars Observer Camera Project Caltech Planetary Sciences Division
Ground Support Engineering, programmer
More information about the Comp.lang.c
mailing list