Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10.1 6/24/83; site alice.UUCP Path: utzoo!watmath!clyde!burl!ulysses!allegra!alice!ark From: ark@alice.UucP (Andrew Koenig) Newsgroups: net.lang.c Subject: Re: if (p), where p is a pointer Message-ID: <4300@alice.UUCP> Date: Wed, 11-Sep-85 11:05:04 EDT Article-I.D.: alice.4300 Posted: Wed Sep 11 11:05:04 1985 Date-Received: Thu, 12-Sep-85 12:19:23 EDT References: <118@mit-hector.UUCP> Organization: Bell Labs, Murray Hill Lines: 40 > With all this talk about NULL pointers not necessarily being equal to 0, > I'm no longer sure what is and isn't portable code. An C idiom I see > (and write) frequently is > *ptr; > ... > if (ptr) > > Will this work correctly on a machine where NULL is not 0? Does it really > need to say > if (ptr != NULL) No, you don't need to say NULL explicitly. The relevant rules are: 1. The only integer that is guaranteed to be converted meaningfully to a pointer is the constant 0: this is guaranteed to yield a value that is distinct from any valid pointer and is guaranteed to compare unequal to any valid pointer. 2. Since 0 is the only distinctive pointer value, it is usually used by convention to mark the end of a list or to indicate a non-existent pointer value. This convention is institutionalized by defining NULL as 0 in . 3. If e is an expression, if(e) and if((e)!=0) always mean exactly the same thing. Thus, if NULL is defined as 0, if (pointer) ... and if (pointer != NULL) ... mean the same thing and the usage is portable. If NULL is not defined as 0, the very definition of NULL is non-portable.