if (p == kInvalid)
I think it is valid C code. Am I overlooking something?
Carsten Hansen
enum {
kInvalid = 0
};
void foo(void);
int main(void)
{
long *p = kInvalid;
if (p == kInvalid)
foo();
return 0;
}
I think you're right.
Both the assignment and the comparison are illegal in C++, but legal in C.
But the compiler complains about the comparison when compiled as C. (It
complains about both the assignment and comparison when compiled as C++.)
You should report the bug to Metrowerks. Be sure to point out that it's only
when compiling as C, not as C++.
-- Darin
No, it isn't.
> enum {
> kInvalid = 0
> };
>
> void foo(void);
>
> int main(void)
> {
> long *p = kInvalid;
>
> if (p == kInvalid)
try:
if ( p == (long *) kInvalid)
instead.
Could you elaborate. The way I see it, kInvalid is an integral constant with
the value 0. The ANSI C standard explicitly allow one to compare a pointer
(of any type) for equality with 0 (ANSI C 3.2.2.3).
Carsten Hansen
>From Section 6.4 of the ANSI/ISO C Standard:
"An integral constant expression shall have integral type and shall only
have operands that are integer constants, enumeration constants, ..."
>From Section 6.2.2.3 of the ANSI/ISO C Standard:
"An integral constant expression with the value 0, or such an expression
cast to type void *, is called a null pointer constant. If a null
pointer constant is assigned to or compared for equality to a pointer,
the constant is converted to a pointer of that type."
In the above code, kInvalid is by definition an integral constant
expression, and therefore it is also a null pointer constant. The above
code is valid ANSI/ISO C, and the compiler should not generate
an error.
Note that all of the the following examples are legal C:
if (p == '-'-'-')
foo();
if (p == 3/4)
foo();
if (p == 0L)
foo();
if (p == UINT_MAX+1)
foo();
--Matt Brunk