enum

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

1. Christopher Benson-ManicaGuest

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

2. Eric SosmanGuest

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

3. Ed MortonGuest

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
4. Christopher Benson-ManicaGuest

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
5. Eric SosmanGuest

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
6. Peter NilssonGuest

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
7. CBFalconerGuest

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.

CBFalconer, Oct 23, 2003
8. Christopher Benson-ManicaGuest

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
9. Scott FrazerGuest

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

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