Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Enum compiler bug?

0 views
Skip to first unread message

Carsten Hansen

unread,
Aug 25, 1999, 3:00:00 AM8/25/99
to
The following code generates a compiler error on the line

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;
}

Darin Adler

unread,
Aug 25, 1999, 3:00:00 AM8/25/99
to
Carsten Hansen <hans...@worldnet.att.net> wrote:
> The following code generates a compiler error on the line
>
> if (p == kInvalid)
>
> I think it is valid C code. Am I overlooking something?
>
>
> 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

Jean-Francois Brouillet

unread,
Aug 26, 1999, 3:00:00 AM8/26/99
to
> The following code generates a compiler error on the line
>
> if (p == kInvalid)
>
> I think it is valid C code. Am I overlooking something?

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.

--
jean-franco...@virgin.net

Carsten Hansen

unread,
Aug 26, 1999, 3:00:00 AM8/26/99
to
In article <7q412a$4o1$1...@nclient13-gui.server.virgin.net>, "Jean-Francois
Brouillet" <jean-franco...@virgin.net> wrote:


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

Matt Brunk

unread,
Aug 26, 1999, 3:00:00 AM8/26/99
to
Carsten Hansen wrote:
>
> The following code generates a compiler error on the line
>
> 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;
> }

>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

0 new messages