Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP
Path: utzoo!mnetor!uunet!seismo!ut-sally!husc6!mit-eddie!ll-xn!ames!oliveb!sun!gorodish!guy
From: guy%gorodish@Sun.COM (Guy Harris)
Newsgroups: comp.lang.c
Subject: Re: NULL, zero, and readable code
Message-ID: <24149@sun.uucp>
Date: Fri, 24-Jul-87 13:58:27 EDT
Article-I.D.: sun.24149
Posted: Fri Jul 24 13:58:27 1987
Date-Received: Sat, 25-Jul-87 15:17:48 EDT
References: <8170@brl-adm.ARPA> <44200004@uicsrd> <6107@brl-smoke.ARPA> <802@cpocd2.UUCP>
Sender: news@sun.uucp
Lines: 43

> Now let Null be a null pointer.  Just because ((char *) 0) is a null
> pointer, you cannot conclude that ((int) Null) == 0.  The ineluctable
> conclusion is that an implementation of C, in which ((int)((char *) 0))
> != 0, is legal.  This might mean that widely used tests such as
> 
> 	if (p)
> 
> where p is a pointer, are simply wrong!

No, it CANNOT mean that!  The test

	if (p)

tests whether "p" is not equal to the zero *of the type that "p" is*!
In other words, if "p" is of an integral type, it tests whether "p"
is not equal to an integral zero; if "p" is of a floating-point type,
it tests whether "p" is not equal to a floating-point zero; and, if
"p" is of a pointer type, it tests whether "p" is not equal to a
"pointer zero" of that type - namely, a NULL pointer.

Were there a "typeof" "operator", another way to say this would be:

	if (p)

is equivalent to

	if (p != (typeof p)0)

> A further interesting question is whether all null pointers must be
> equal.  K & R is silent on this question, although they don't mention
> more than one.  If not, then just because ((char *) 0) is a null
> pointer, you cannot conclude that ((char *) 0) == ((int *) 0), and tests
> like
> 
> 	if (p == NULL)
> 
> might even fail for some null pointers!  Not a very pleasant thought.

If NULL is properly defined as "0", rather than improperly defined as
"(char *) 0", this test will not fail for any null pointers.
	Guy Harris
	{ihnp4, decvax, seismo, decwrl, ...}!sun!guy
	guy@sun.com