Path: utzoo!utgpu!water!watmath!clyde!att!pacbell!ames!elroy!cit-vax!beckenba From: beckenba@cit-vax.Caltech.Edu (Joe Beckenbach) Newsgroups: comp.lang.c Subject: Re: Null pointers and constant zero Message-ID: <7180@cit-vax.Caltech.Edu> Date: 7 Jul 88 01:53:42 GMT References: <402@proxftl.UUCP> Reply-To: beckenba@cit-vax.UUCP (Joe Beckenbach) Organization: California Institute of Technology Lines: 36 In article <402@proxftl.UUCP> bill@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@csvax.caltech.edu Caltech 1-58, Pasadena CA 91125 Mars Observer Camera Project Caltech Planetary Sciences Division Ground Support Engineering, programmer