tech-kern archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Re: NULL pointer arithmetic issues



>> int one(void) { return(1); }
>> then (one()-one()) is not [a null pointer constant]
> As you say, it's an integer expression.  And I read that "or" part as
> just an expression, which this is.  So I believe it is a valid way to
> creating something that can be converted to a NULL pointer.

C99 words it as

       [#3] An integer constant expression with  the  value  0,  or
       such  an  expression  cast  to type void *, is called a null
       pointer   constant.

That little word "such" is important.  As I read it, that means a null
pointer constant is "[a]n integer constant expression with the value 0,
or an integer constant expression with the value 0 cast to type void *"
(not just "an integer expression with the value 0 cast to...").

> if (expression) statement; shall execute statement if expression not
> equals 0, according to the standard.

Yes:

       [#2]  In  both  forms, the first substatement is executed if
       the expression compares unequal to 0.  In the else form, the
       second  substatement  is executed if the expression compares
       equal to 0.  If the first  substatement  is  reached  via  a
       label, the second substatement is not executed.

> So, where does that leave this code:

> char *p;
> if (p) foo();

> p is not an integer.  How do you compare it to 0?

The same way you do in

if (p != 0) foo();

How else?

I would admittedly prefer slightly more verbose wording, saying
explicitly that the 0 to which the control expression is compared is,
when applicable, a null pointer constant.  (Comparison of a pointer
with a null pointer constant is specifically permitted for == and != -
see 6.5.9.)

/~\ The ASCII				  Mouse
\ / Ribbon Campaign
 X  Against HTML		mouse%rodents-montreal.org@localhost
/ \ Email!	     7D C8 61 52 5D E7 2D 39  4E F1 31 3E E8 B3 27 4B


Home | Main Index | Thread Index | Old Index