Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10.2 9/18/84; site smeagol.UUCP Path: utzoo!linus!decvax!decwrl!greipa!pesnta!hplabs!sdcrdcf!oberon!smeagol!ross From: ross@smeagol.UUCP (Gary_Ross) Newsgroups: net.lang.c Subject: Re: RE: if(p) Message-ID: <484@smeagol.UUCP> Date: Sun, 13-Oct-85 18:51:27 EDT Article-I.D.: smeagol.484 Posted: Sun Oct 13 18:51:27 1985 Date-Received: Thu, 17-Oct-85 20:54:47 EDT References: <1671@brl-tgr.UUCP> <30000017@ISM780.UUCP> Organization: Spacecraft Data Systems group @ JPL, Pasadena, CA, USA Lines: 41 > > section 9.3, I draw the inference that if p is a pointer then: > > if(p) statement > > is not a valid construct in the C language and therefore questions of > portability are rendered moot. > > My reasoning is as follows. The phrase, "In both cases the expression is > evaluated and if it is non-zero" does not imply comparison to zero. Instead > it implies that the expression must be of a numeric type since only numeric > data have the property of 'zeroness'. The expression consting only of a > pointer variable evaluates either to a pointer to some object or to a pointer > to no object. It does not evaluate to zero or not zero. As I understand it, the C compiler interprets pointers as integers in terms of evaluating an if statement. This means that if the value of a pointer is null, the result of: if (p) statement1 else statement2 will execute statement2 because a null pointer has value 0. It is conceptually very nice to think of numbers having numeric properties and pointers having properties of either pointing to an object or not, but when you get right down to it pointers are nothing more than numeric variables containing memory addresses. Therefore the concept of using the value of a pointer in an if statement makes perfect sense especially since the C language is in general not very strict on typing of variables. > Parenthetically, the symbol '0' is overloaded in the C language. In the > context of a pointer it *represents* the constant that points to no object. > in the context of a number it *represents* the constant with the value zero. > i.e., the symbol '0' is not the value of either of the the constants, it is > mearly the *name* used to identify either of the constants in the *source* > from of a program. I disagree. I think it is very concise to have the value '0' represent a false result of a logical operation and the value of an invalid pointer. It seems to me that this makes statements like if(p) neat and simple, as well as obviously requesting the validity of a pointer.