Why NULL is 0
William E. Davidsen Jr
davidsen at steinmetz.steinmetz.ge.com
Tue Apr 5 02:22:19 AEST 1988
There has been a great deal of misunderstanding of the use of zero and
pointers. It seems clear in K&R and practice that assignment of a zero
to a pointer produces a NULL pointer of the appropriate type. What is
incorrectly assumed is that zero *is* a NULL pointer.
People (usually ;>) have no problem with the idea the while assigning a
zero to a float gives it the vaule 0.0, in most implementations the
float value does not have all bits set to zero.
There are two places in which a zero will not work as expected:
In expressions, adding an int expression to a pointer produces a
pointer result, while adding an int expression to an int constant (zero)
produces an int result. Since there does not seem to be a good use for
treating the sum of two ints an address in any remotely portable
program, this is of interest but not concern.
In procedure calls, however, on many machines the arguments must have
the correct type for portability because either the size of int and
pointer are not the same, or because the size and content of various
pointer types are not the same.
If prototypes are used, then coding need not be as precise, since use
of 0 where 0L is needed, or zero where (char *)0 is needed will be
corrected by the compiler. Without prototypes the arguments will need to
be correct if the program is to be portable.
There are many programs which "have worked for years" which are not
portable, because of this lack of typing on arguments. Most of these run
on any machine which has the size of int equal sizeof pointer, and all
pointers are the same in content. This includes the VAX and 68000
family. Other machines, such as some Data General models, Cray, small
Intel processors, SPARC, and some non-UNIX C compilers on any machine
will not accept this lack of explicit typing.
--
bill davidsen (wedu at ge-crd.arpa)
{uunet | philabs | seismo}!steinmetz!crdos1!davidsen
"Stupidity, like virtue, is its own reward" -me
More information about the Comp.lang.c
mailing list