if(p)

marv at ISM780.UUCP marv at ISM780.UUCP
Wed Oct 9 05:54:00 AEST 1985


>/* Written  7:21 pm  Oct  7, 1985 by gwyn at brl-tgr in ISM780:net.lang.c */
>>     Section 9.3 Conditional statement:
>>       if ( expression ) statement
>>       if ( expression ) statement else statement
>>     In both cases the expression is evaluated and if it is non-zero,
                                                    ^^^^^^^^^^^^^^^^^
>This implies a comparison against 0.  Therefore if "expression" is a
>pointer, this is comparison of a pointer with 0, which is the special
>case previously discussed.  There is no concept of "evaluates to zero"
>independently of "comparison with zero".

Just to add more flames to this theological discussion, from my reading of
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.

On the other hand, the expression p==0 is an expression of type int. In fact
it evaluates to the integer 0 or 1 (K&R section 7.6 Relational operators).

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 also suggest that if we take the stance that "evaluates to zero" implies
comparison to zero we could also claim that:
    i = 0;
    if (p == i)
is equivalent to:
   if (p == 0)
and I believe that most C theologians would reject the above equivalence.

The fact the K&R is very fuzzy about the type of an expression (as opposed to
the types of primitive operands) leads to many a theological discussion.

George Bool sure simplified my life,
	  Marv Rubinstein  -- Interactive Systems



More information about the Comp.lang.c mailing list