enum...

S

Steve

Hi,

in my header file I write:

typedef enum
{
RED,
GREEN,
BLUE
} Colors;


and in my C-source file I write:

static Colors color = RED;


Unfortunately I get a compiler error:
syntax error : identifier 'color'

Why? What is the correct way of using an enum?

Steve
 
S

Steve

Just found out that the header I thought was inluded in fact was not
included!
After fixing so correct header was included it worked fine.
/Steve
 
M

Michael Mair

Steve said:
Hi,

in my header file I write:

typedef enum
{
RED,
GREEN,
BLUE
} Colors;


and in my C-source file I write:

static Colors color = RED;


Unfortunately I get a compiler error:
syntax error : identifier 'color'

Why? What is the correct way of using an enum?

It is a good idea to provide a minimal example of the misbehaviour;
this probably helps you find the error before posting but it
certainly helps us to help you.

This works for me:

#include <stdio.h>

typedef enum {
RED=0, GREEN, BLUE, COL_END
} Colors;

char *colornames[COL_END] = {"red","green","blue"};

int main (void)
{
Colors color = RED;

printf("Selected color %s.\n",colornames[color]);

return 0;
}

You can static qualify color and replace RED=0 by RED, of course.
FYI: In C99, you can also use
char *colornames[COL_END] = {
[RED]="red",
[GREEN]="green",
[BLUE]="blue"
};

If I move the typedef to a header file and #include it, nothing
changes.

Probably, your mistake is somewhere else in your code.


Cheers
Michael
 
C

Chris Croughton

On Thu, 27 Jan 2005 10:27:59 +0100, Steve

Plaese don't top-post (you should put comments after the thing to which
they refer).
Just found out that the header I thought was inluded in fact was not
included!

If I had a penny for every time I've done that, I'd be able to buy a
beer! Or even buy everyone on c.l.c a beer...
After fixing so correct header was included it worked fine.

And so it should.

Incidentally, running your compiler to get just preprocessed output (-E
on many compilers) and looking at that often shows that sort of error.
In your case it would have been obvious that the enum wasn't declared.
The same for multiply defined symbols, it helps to track down which
headers were defining them (especially if your preprocessor has an
option to leave #defines in the code, like gcc's -dD option).

Chris C
 
M

Martin Ambuhl

Steve said:
in my header file I write:
typedef enum
{
RED,
GREEN,
BLUE
} Colors;
and in my C-source file I write:
static Colors color = RED;
Unfortunately I get a compiler error:
syntax error : identifier 'color'

There is no way to tell from your description. Post a minimal actual
program that demonstrates the error.

What is the correct way of using an enum?

As far as can be told from your description, the way you did. Try the
following and observe what happens:

#include <stdio.h>

typedef enum
{
RED,
GREEN,
BLUE
} Colors;


static Colors color = RED;

int main(void)
{
static Colors color2 = BLUE;
printf("[output]\n"
"color (RED) = %d\n"
"color2 (BLUE) = %d\n", color, color2);
return 0;
}

[output]
color (RED) = 0
color2 (BLUE) = 2
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top