Header problems
Alan J Rosenthal
flaps at dgp.toronto.edu
Thu Mar 10 03:20:17 AEST 1988
Doug Gwyn <gwyn at brl.arpa> wrote about NULL, and said that the proper
definition of it is ``#define NULL 0''.
dag at 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?
More information about the Comp.lang.c
mailing list