Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP
Path: utzoo!mnetor!uunet!seismo!rutgers!labrea!decwrl!pyramid!oliveb!intelca!mipos3!cpocd2!howard
From: howard@cpocd2.UUCP (Howard A. Landman)
Newsgroups: comp.lang.c
Subject: Re: NULL, zero, and readable code
Message-ID: <802@cpocd2.UUCP>
Date: Thu, 23-Jul-87 19:58:51 EDT
Article-I.D.: cpocd2.802
Posted: Thu Jul 23 19:58:51 1987
Date-Received: Sat, 25-Jul-87 13:45:12 EDT
References: <8170@brl-adm.ARPA> <44200004@uicsrd> <6107@brl-smoke.ARPA>
Reply-To: howard@cpocd2.UUCP (Howard A. Landman)
Organization: Intel Corp. ASIC Systems Organization, Chandler AZ
Lines: 48

In article <6107@brl-smoke.ARPA> gwyn@brl.arpa (Doug Gwyn (VLD/VMB) ) writes:
>In article <44200004@uicsrd> schouten@uicsrd.cs.uiuc.edu writes:
>>I have worked on systems where NULL was != 0, because 0 was a valid address.
>
>I thought we were discussing C.  No C data object is permitted to have
>an address such that a pointer to it is indistinguishable from 0 cast
>to the appropriate pointer type.

This is true but doesn't contradict the previous statement.  The exact
statement in K & R is:

	...it is guaranteed that the assignment of the constant 0 to
	a pointer will produce a null pointer distinguishable from a
	pointer to any object.

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!  From K & R we see that if
statements are just tests for zero:

	"...the expression is evaluated and if it is non-zero, the
	first substatement is executed."  p.201

	"...relational expressions like i > j and logical expressions
	connected by && and || are defined to have value 1 if true,
	and 0 if false.  ...  (In the test part of an if, while, for,
	etc., ``true'' just means ``non-zero.'')" p.41

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.

-- 
	Howard A. Landman
	...!{oliveb,...}!intelca!mipos3!cpocd2!howard
	howard%cpocd2%sc.intel.com@RELAY.CS.NET
	"..., precisely the opposite of what we now know to be true!"