What C compilers have non-zero null pointers?
Andrew Koenig
ark at alice.UUCP
Thu Jul 19 03:54:54 AEST 1990
In article <12288 at netcom.UUCP>, ergo at netcom.UUCP (Isaac Rabinovitch) writes:
> True. But what the "NULL should always be 0" diehards want is not to
> write (for example)
> for (ptr = fist; ptr != 0; ptr = ptr->next)
> in which 0 should probably be #DEFINED anyway, but rather
> for (ptr = first; ptr ; ptr = ptr->next)
These two forms are guaranteed to be equivalent (if you change `fist'
to `first' in the first example). Period.
> which produces tighter code and (most important of all) looks
> spiffier. It has the elegance of expression old C hands crave.
Whether it produces tighter code is a matter between you and your
local implementation. Since the two forms are equivalent, there is
no particular reason to believe that they will produce different code
at all.
> My '78 K&R says that assigning 0 to a pointer is (or was) guarranteed
> to produce a NULL, even on compilers that didn't like other
> integer-to-pointer assignments. But, interestingly, they did *not*
> guarantee, even then, the reverse!
Yes indeed. However, if you write
ptr != 0
then the 0 is converted to a pointer of the appropriate type and then
compared, and that *is* guaranteed to work.
--
--Andrew Koenig
ark at europa.att.com
More information about the Comp.lang.c
mailing list