• C

enum type position

Can I use an enumerated type without instantiating? For example, I would like to get the array position of each enumeration, and use the value directly.

For example, if I have the following enumerations:

enum WORDS {FM5075D, MT0012, MT0013, MT5005A, DS5041};

I would like to be able to do something like (or hopefully simpler!):

void loadheader(unsigned short rec_array[], unsigned short recordnum)
   enum WORDS w;

   write_bitrange(&rec_array[w.FM5075D],  04, 06, SUBSYS_DISCRIM);
   write_bitrange(&rec_array[w.MT0013], 00, 15, recordnum);

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

In C enumerated type constants and integers are interchangable. So you can use an enum constant wherever you can use an integer constant. Here is your example with some minor corrections:

int loadheader(unsigned short rec_array[], unsigned short recordnum)
   enum WORDS w;

   write_bitrange(&rec_array[FM5075D], 04, 06, SUBSYS_DISCRIM);
   write_bitrange(&rec_array[MT0013], 00, 15, recordnum);

   return FILE_FOUND;

Note that you cannot give the same name to two different enum constants, even if they are part of different eum types. For example, the following is illegal:

enum FontStyle {BOLD,ITALIC,NONE};

since `NONE' is defined twice.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
lhgarrettAuthor Commented:
Excellently explained. Thanks!!!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.