Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!seismo!rutgers!dayton!viper!john From: john@viper.UUCP (John Stanley) Newsgroups: comp.lang.c Subject: Re: pointer debate raging on... (LONG) Message-ID: <917@viper.UUCP> Date: Fri, 1-May-87 16:16:48 EDT Article-I.D.: viper.917 Posted: Fri May 1 16:16:48 1987 Date-Received: Sun, 3-May-87 01:53:31 EDT References: <149@sds.UUCP> <17446@sun.uucp> Reply-To: john@viper.UUCP (John Stanley) Organization: DynaSoft Systems Lines: 52 In article <17446@sun.uucp> guy@sun.UUCP (Guy Harris) writes: > >>Applaud, applaud, applaud. John has correctly pointed out that >>sizeof(pointer) == sizeof(int) is not the universal constant that >>many people believe it to be. It is for this exact reason that >>NULL should ***NOT*** be defined as 0. > >OK, now given that > > sizeof (char *) == sizeof (int *) > >is also not a tautology, what should NULL be defined as? (char *)0 >won't cut it, nor will (int *)0. The answer to this was given in my original reply. The only pointer type which is data nonspecific is void*, thus NULL should be ((void*)0). I have used this on enough projects to know it works and have only had problems with porting a bug infested program where the author used NULL every place you or I would have used a 0. (It turned out that the program was originaly written by a college sophmore who had only had one class in C... He wrote it on a machine where sizeof a pointer and an int were the same...) I've looked into this quite a bit in the last 2 weeks and have come to the conclusion that K&R made a mistake. (gasp :) Dispite the fact that they constantly and consistantly refer to NULL as a "pointer", they fail to ever pass NULL as a parameter in the examples (something that would have brought this problem to light much earlier) and they consistantly use 0 to #define NULL rather than a pointer. This combined with their only having access to machines where pointers were the same size as ints has caused the current state of confusion. The clearest example of this is K&R page 192 in the first paragraph. "The compilers currently allow a pointer to be assigned to an integer, an integer to be assigned to a pointer, and a pointer to a pointer of another type. The assignment is a pure copy operation with no conversions." They go on to point out that this assumption is non- portable...unfortunately, they failed to say why or to deal with the ambiguity of the integer constant 0 being used as if it were really a pointer. While this doesn't effect a majority of applications or systems, the few where it does require a re-examination of NULL and a redefinition of the "pointer constant" to something that is in -all- respects a -true- pointer constant.... (Please, no jokes about my reply being about a subject that's "NULL and void*"... :-) --- John Stanley (john@viper.UUCP) Software Consultant - DynaSoft Systems UUCP: ...{amdahl,ihnp4,rutgers}!{meccts,dayton}!viper!john