Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

enum

8 views
Skip to first unread message

Christopher Benson-Manica

unread,
Oct 22, 2003, 2:32:23 PM10/22/03
to
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.

Eric Sosman

unread,
Oct 22, 2003, 3:31:52 PM10/22/03
to
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....@sun.com

Ed Morton

unread,
Oct 22, 2003, 3:58:28 PM10/22/03
to

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.

Christopher Benson-Manica

unread,
Oct 22, 2003, 4:59:01 PM10/22/03
to
Eric Sosman <Eric....@sun.com> 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 ;(

Eric Sosman

unread,
Oct 22, 2003, 5:04:39 PM10/22/03
to
Christopher Benson-Manica wrote:
>
> Eric Sosman <Eric....@sun.com> 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....@sun.com

Peter Nilsson

unread,
Oct 22, 2003, 9:25:25 PM10/22/03
to
Christopher Benson-Manica <at...@nospam.cyberspace.org> wrote in message news:<bn6ifn$epq$1...@chessie.cirr.com>...

> 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[i].name,
footype_array[i].value);
}

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

return 0;
}

--
Peter

CBFalconer

unread,
Oct 22, 2003, 9:33:46 PM10/22/03
to
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 (cbfal...@yahoo.com) (cbfal...@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!

Christopher Benson-Manica

unread,
Oct 23, 2003, 8:21:08 AM10/23/03
to
Eric Sosman <Eric....@sun.com> 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.

Scott Frazer

unread,
Oct 23, 2003, 8:44:52 AM10/23/03
to
Christopher Benson-Manica <at...@nospam.cyberspace.org> wrote in message news:<bn6r2l$fm4$1...@chessie.cirr.com>...

> Eric Sosman <Eric....@sun.com> 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[i];
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

0 new messages