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