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

Discussion in 'C Programming' started by Troy Hanson, Sep 12, 2003.

  1. Troy Hanson

    Troy Hanson Guest

    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;
    Troy Hanson, Sep 12, 2003
    #1
    1. Advertising

  2. Troy Hanson

    Ben Pfaff Guest

    (Troy Hanson) writes:

    > 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
    --
    "This is a wonderful answer.
    It's off-topic, it's incorrect, and it doesn't answer the question."
    --Richard Heathfield
    Ben Pfaff, Sep 12, 2003
    #2
    1. Advertising

  3. Troy Hanson

    Troy Hanson Guest

    Ben Pfaff <> wrote in message news:<>...
    > (Troy Hanson) writes:
    >
    > > 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.
    Troy Hanson, Sep 13, 2003
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. -

    enum within an enum

    -, Jun 12, 2005, in forum: Java
    Replies:
    6
    Views:
    540
  2. Jerminia
    Replies:
    3
    Views:
    624
    Roedy Green
    Oct 7, 2005
  3. Ernst Murnleitner

    How to enum an enum?

    Ernst Murnleitner, Nov 12, 2003, in forum: C++
    Replies:
    5
    Views:
    463
    Rolf Magnus
    Nov 13, 2003
  4. Dead RAM
    Replies:
    20
    Views:
    1,111
    John Harrison
    Jul 14, 2004
  5. Ben

    Strings, Strings and Damned Strings

    Ben, Jun 22, 2006, in forum: C Programming
    Replies:
    14
    Views:
    756
    Malcolm
    Jun 24, 2006
Loading...

Share This Page