enum

  • Thread starter Christopher Benson-Manica
  • Start date
C

Christopher Benson-Manica

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 */
....
 
E

Eric Sosman

Christopher said:
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.
 
E

Ed Morton

Christopher said:
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.
 
C

Christopher Benson-Manica

Eric Sosman said:
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 ;(
 
E

Eric Sosman

Christopher said:
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.
 
P

Peter Nilsson

Christopher Benson-Manica said:
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;
}
 
C

CBFalconer

Ed said:
Christopher said:
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));
 
C

Christopher Benson-Manica

Eric Sosman said:
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.
 
S

Scott Frazer

Christopher Benson-Manica said:
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
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top