Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cycle every item in an enum

Tags:

c

Is it possible to cycle and extract its value for every item in an enum, e.g:

enum {
   item 1 = 1,
   item 2 = 7,
   item 3 = 'B'
} items;

To say, an array. Possible?

like image 975
freonix Avatar asked Dec 04 '22 08:12

freonix


1 Answers

Do you mean to construct an array with one element for each value of the enum? Such that you would end up with an array {1, 7, 'B'}?

Not possible. An enum is not much more than a bunch of compile-time constants. At runtime, they are effectively just ints. The compiler doesn't even check to make sure you are putting valid values in your enum variable (you could put the value 5 in there and it wouldn't mind). So it has no knowledge of what the possible enum values are at runtime.

Of course, if your enum is contiguous (say, you had defined an enum for the values 0, 1, 2 and 3), then you can do this using a for loop.

like image 52
mgiuca Avatar answered Dec 21 '22 15:12

mgiuca