enum

Discussion in 'C Programming' started by Christopher Benson-Manica, Oct 22, 2003.

  1. Given an enumeration something like

    enum footype {
    foo=3,
    bar=5,
    baz=17,
    quux=200 };

    is there a fast way to access all four of these values? I.e., given

    enum footype m=foo;

    printf( "%d\n", m ); /* print 3 */
    /* magical things */
    printf( "%d\n", m ); /* print 5 */
    /* wondrous things */
    printf( "%d\n", m ); /* print 17 */
    ....

    --
    Christopher Benson-Manica | I *should* know what I'm talking about - if I
    ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
     
    Christopher Benson-Manica, Oct 22, 2003
    #1
    1. Advertising

  2. Christopher Benson-Manica

    Eric Sosman Guest

    Christopher Benson-Manica wrote:
    >
    > Given an enumeration something like
    >
    > enum footype {
    > foo=3,
    > bar=5,
    > baz=17,
    > quux=200 };
    >
    > is there a fast way to access all four of these values? I.e., given
    >
    > enum footype m=foo;
    >
    > printf( "%d\n", m ); /* print 3 */
    > /* magical things */
    > printf( "%d\n", m ); /* print 5 */
    > /* wondrous things */
    > printf( "%d\n", m ); /* print 17 */


    For "magical things," use `m = bar;'. For "wondrous
    things," use `m = baz;'. If what you were hoping for is
    some kind of a `++' that would take `m' directly from `baz'
    to `quux', you're out of luck.

    Of course, there's always

    const enum footype next_foo[] = {
    0, 0, 0, bar, 0, baz, 0, ... };
    m = next_foo[m];

    .... but that doesn't seem too attractive.

    --
     
    Eric Sosman, Oct 22, 2003
    #2
    1. Advertising

  3. Christopher Benson-Manica

    Ed Morton Guest

    Christopher Benson-Manica wrote:

    > Given an enumeration something like
    >
    > enum footype {
    > foo=3,
    > bar=5,
    > baz=17,
    > quux=200 };
    >
    > is there a fast way to access all four of these values? I.e., given
    >
    > enum footype m=foo;
    >
    > printf( "%d\n", m ); /* print 3 */
    > /* magical things */
    > printf( "%d\n", m ); /* print 5 */
    > /* wondrous things */
    > printf( "%d\n", m ); /* print 17 */
    > ...


    Best I can come up with is:

    #include <stdio.h>

    enum footype {
    foo=3,
    bar=5,
    baz=17,
    quux=200 };

    int main() {

    const enum footype m[] = { foo, bar, baz, quux };
    int i = 0;

    printf( "%d\n", m[i++] ); /* print 3 */
    printf( "%d\n", m[i++] ); /* print 5 */
    printf( "%d\n", m[i++] ); /* print 17 */

    return 0;
    }

    Regards,

    Ed.
     
    Ed Morton, Oct 22, 2003
    #3
  4. Eric Sosman <> spoke thus:

    > If what you were hoping for is
    > some kind of a `++' that would take `m' directly from `baz'
    > to `quux', you're out of luck.


    I figured as much (enums are just integers, after all), but I wanted to see if
    there were any "tricks" I could use in place of manually specifying 15 or so
    enumerated values. It won't be particularly fun ;(

    --
    Christopher Benson-Manica | I *should* know what I'm talking about - if I
    ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
     
    Christopher Benson-Manica, Oct 22, 2003
    #4
  5. Christopher Benson-Manica

    Eric Sosman Guest

    Christopher Benson-Manica wrote:
    >
    > Eric Sosman <> spoke thus:
    >
    > > If what you were hoping for is
    > > some kind of a `++' that would take `m' directly from `baz'
    > > to `quux', you're out of luck.

    >
    > I figured as much (enums are just integers, after all), but I wanted to see if
    > there were any "tricks" I could use in place of manually specifying 15 or so
    > enumerated values. It won't be particularly fun ;(


    Perhaps if you described what you're trying to do -- why
    do you want to step through a sparse set of values? -- someone
    might suggest a better way to do it.

    --
     
    Eric Sosman, Oct 22, 2003
    #5
  6. Christopher Benson-Manica <> wrote in message news:<bn6ifn$epq$>...
    > Given an enumeration something like
    >
    > enum footype {
    > foo=3,
    > bar=5,
    > baz=17,
    > quux=200 };
    >
    > is there a fast way to access all four of these values? I.e., given
    >
    > enum footype m=foo;
    >
    > printf( "%d\n", m ); /* print 3 */
    > /* magical things */
    > printf( "%d\n", m ); /* print 5 */
    > /* wondrous things */
    > printf( "%d\n", m ); /* print 17 */
    > ...


    Not pretty, but...

    #define as_enum(name,value) name = value
    #define as_array(name,value) { #name, value }

    #define comma ,

    #define my_enum(macro,_) \
    macro( foo, 3 ) _ \
    macro( bar, 5 ) _ \
    macro( baz, 17 ) _ \
    macro( quux, 200 )

    enum footype { my_enum(as_enum, comma) };

    struct { const char *name; int value; }
    footype_array[] = { my_enum(as_array, comma) };


    #include <stdio.h>

    #define NELEM(x) (sizeof (x) / sizeof *(x))

    #define DUMP(x) printf("%s = %d\n", #x, x);

    int main(void)
    {
    size_t i;

    puts("As iterated array...");
    for (i = 0; i < NELEM(footype_array); i++)
    {
    printf("%s = %d\n", footype_array.name,
    footype_array.value);
    }

    puts("\nAs enum identifiers...");
    DUMP(foo);
    DUMP(bar);
    DUMP(baz);
    DUMP(quux);

    return 0;
    }

    --
    Peter
     
    Peter Nilsson, Oct 23, 2003
    #6
  7. Christopher Benson-Manica

    CBFalconer Guest

    Ed Morton wrote:
    > Christopher Benson-Manica wrote:
    >
    > > Given an enumeration something like
    > >
    > > enum footype {
    > > foo=3,
    > > bar=5,
    > > baz=17,
    > > quux=200 };
    > >
    > > is there a fast way to access all four of these values? I.e., given
    > >

    .... snip ...
    >
    > Best I can come up with is:
    >
    > #include <stdio.h>
    >
    > enum footype {
    > foo=3,
    > bar=5,
    > baz=17,
    > quux=200 };
    >
    > int main() {
    > const enum footype m[] = { foo, bar, baz, quux };
    > int i = 0;
    > printf( "%d\n", m[i++] ); /* print 3 */
    > printf( "%d\n", m[i++] ); /* print 5 */
    > printf( "%d\n", m[i++] ); /* print 17 */
    > return 0;
    > }


    The most useful I can come up with is:

    const char *fooname(enum footype val) {
    switch (val) {
    case foo: return "foo";
    case bar: return "bar";
    case bas: return "baz";
    case quux: return "quux";
    default: return "Illegal value";
    }
    }

    and then we can use statements such as:

    printf("%s\n", fooname(m));

    --
    Chuck F () ()
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net> USE worldnet address!
     
    CBFalconer, Oct 23, 2003
    #7
  8. Eric Sosman <> spoke thus:

    > Perhaps if you described what you're trying to do -- why
    > do you want to step through a sparse set of values? -- someone
    > might suggest a better way to do it.


    Well, the "why" is probably clear - someone else's code uses sparse enumerated
    values as constants like

    enum idtype {
    id1=1;
    id2=2;
    ...
    id9=9;
    id10=19;
    ...
    id22=1065; /* actual value, unfortunately */
    };

    and I have a situation where I'd like to hit all of them.

    --
    Christopher Benson-Manica | I *should* know what I'm talking about - if I
    ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
     
    Christopher Benson-Manica, Oct 23, 2003
    #8
  9. Christopher Benson-Manica

    Scott Frazer Guest

    Christopher Benson-Manica <> wrote in message news:<bn6r2l$fm4$>...
    > Eric Sosman <> spoke thus:
    >
    > > If what you were hoping for is
    > > some kind of a `++' that would take `m' directly from `baz'
    > > to `quux', you're out of luck.

    >
    > I figured as much (enums are just integers, after all), but I wanted to see if
    > there were any "tricks" I could use in place of manually specifying 15 or so
    > enumerated values. It won't be particularly fun ;(


    How about a lookup table?

    enum footype {
    foo=3,
    bar=5,
    baz=17,
    quux=200
    };

    const enum footype footype_lut[] = {
    foo,
    bar,
    baz,
    quux
    };

    #define NUM_FOOTYPE_ELEMENTS (sizeof(footype_lut)/sizeof(enum
    footype))

    Then loop through the LUT:

    for( i = 0; i < NUM_FOOTYPE_ELEMENTS; i++ ) {
    m = footype_lut;
    printf( ... );
    }

    Still kind of redundant, but not so bad if you don't have a ton of
    elements and/or they don't change much.

    Scott
     
    Scott Frazer, Oct 23, 2003
    #9
    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:
    548
  2. Jerminia
    Replies:
    3
    Views:
    633
    Roedy Green
    Oct 7, 2005
  3. Ernst Murnleitner

    How to enum an enum?

    Ernst Murnleitner, Nov 12, 2003, in forum: C++
    Replies:
    5
    Views:
    469
    Rolf Magnus
    Nov 13, 2003
  4. mrhicks
    Replies:
    2
    Views:
    427
    Dave Thompson
    Jun 10, 2004
  5. Randy
    Replies:
    1
    Views:
    520
    David Harmon
    Jan 7, 2006
Loading...

Share This Page