Newsgroups: comp.lang.c Path: utzoo!utgpu!jarvis.csri.toronto.edu!dgp.toronto.edu!flaps From: flaps@dgp.toronto.edu (Alan J Rosenthal) Subject: Re: Header problems Message-ID: <8803091720.AA09301@explorer.dgp.toronto.edu> Organization: University of Toronto References: <2550049@hpisod2.HP.COM> <7412@brl-smoke.ARPA> <3351@chinet.UUCP> Date: Wed, 9 Mar 88 12:20:17 EST Doug Gwyn wrote about NULL, and said that the proper definition of it is ``#define NULL 0''. dag@chinet.UUCP (Daniel A. Glasser) wrote: >Your use of NULL === 0 promotes unportable code ... [moralizing deleted] > >On machines were sizeof int != sizeof(void *), the above definition will >not work on older style function calls (without prototypes) or in var-arg >situations. Requiring sizeof int == sizeof(void *) is not a viable >solution... > > #define NULL ((void *)0) >On older compilers, replace 'void' with 'char'. This allows the use of >a short NULL representation when the compiler is smart enough to use it >but forces passing of NULL in argument lists as a pointer sized object. This doesn't require sizeof(int) == sizeof(void *). The assumption is that you always cast NULL before passing it. Casting is still required if NULL is defined as ((void *)0); you can't assume that all pointers have the same size or representation. Passing a pointer to void to a routine that expects a pointer to struct gosh is as big a mistake as passing an int to a routine that expects a pointer to struct gosh. By the way, if you call f(0) where f expects a char *, the requirement for it to work is not simply that sizeof(int) == sizeof(char *), but also that (int)0 and (char *)0 have the same bit representation. With respect to your phrase "a pointer sized object" - there is no such thing in C. There may happen to be such a thing in a particular implementation of C due to hardware considerations. (Obviously, it is usually advantageous to represent all pointers in the same way (but not always); therefore this is a common occurrence.) ajr -- If you had eternal life, would you be able to say all the integers?