user1128265 user1128265 - 1 year ago 96
C Question

Access an enum using an index in C


enum Test
a = 3,
b = 7,
c = 1

I want to access the enum using an index. Something like this:

for (i=0; i<n; i++)

How can I do something like this, where I will be able to access the enum using an index, though the members of the enum have different values?

Answer Source

This is the best you can do:

enum Test { a = 3, b = 7, c = 1, LAST = -1 };
static const enum Test Test_map[] = { a, b, c, LAST };

for (int i = 0; Test_map[i] != LAST; i++)

You have to maintain the mapping yourself.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download