Associating strings with an enum (can I use a macro?)

T

Troy Hanson

I use this kind of construct sometimes:
typedef enum color {
red,
orange,
blue
} color;

char *colors_strs[] = { "red", "orange", "blue" };

That way, I can easily print the textual name of enum variables, like:
color foreground = blue;
printf("foreground is %s", colors_strs[ foreground ] );

Maybe there's a more clever way to have my enum's associatiable with a
string (any ideas?) - if not, I'd at least like to automate the
generation of my enum and associated char *array[] using a macro. I'm
using GNU cpp.

I'm imagine its usage would look something like
ENUM_STRING( color, red, orange, blue );
that would produce the enum declaration and the color_strs[] above.

I've fiddled with cpp macros but to no avail. I haven't been able to
produce one that will stringify each arg in a vararg list. The closest
I could get, below, stringifies its entire vararg list (you get "red,
orange, blue" as a single string). I'd like to figure out how to get
"red", "orange", "blue".

#define ENUM_STRING( enm, ...) char * enm ## _str = #__VA_ARGS__;
typedef enum enm { __VA_ARGS__ } enm;
 
B

Ben Pfaff

I use this kind of construct sometimes:
typedef enum color {
red,
orange,
blue
} color;

char *colors_strs[] = { "red", "orange", "blue" };

That way, I can easily print the textual name of enum variables, like:
color foreground = blue;
printf("foreground is %s", colors_strs[ foreground ] );

Maybe there's a more clever way to have my enum's associatiable with a
string (any ideas?) - if not, I'd at least like to automate the
generation of my enum and associated char *array[] using a macro. I'm
using GNU cpp.

Something like this (not tested):

#define COLORS \
COLOR(red) \
COLOR(orange) \
COLOR(blue)

#define COLOR(x) x,
typedef enum color {
COLORS
terminator_for_c90 /* may be omitted in C99 */
} color;
#undef COLOR

#define COLOR(x) #x,
char *colors_strs[] = {
COLORS
};
#undef COLOR
 
T

Troy Hanson

Ben Pfaff said:
I use this kind of construct sometimes:
typedef enum color {
red,
orange,
blue
} color;

char *colors_strs[] = { "red", "orange", "blue" };

That way, I can easily print the textual name of enum variables, like:
color foreground = blue;
printf("foreground is %s", colors_strs[ foreground ] );

Maybe there's a more clever way to have my enum's associatiable with a
string (any ideas?) - if not, I'd at least like to automate the
generation of my enum and associated char *array[] using a macro. I'm
using GNU cpp.

Something like this (not tested):

#define COLORS \
COLOR(red) \
COLOR(orange) \
COLOR(blue)

#define COLOR(x) x,
typedef enum color {
COLORS
terminator_for_c90 /* may be omitted in C99 */
} color;
#undef COLOR

#define COLOR(x) #x,
char *colors_strs[] = {
COLORS
};
#undef COLOR

Ah -- good idea. Thanks for your help.
 

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
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top