if(p)

Gary_Ross ross at smeagol.UUCP
Mon Oct 14 08:51:27 AEST 1985


> 
> 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.



More information about the Comp.lang.c mailing list