• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 265
  • Last Modified:

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};
enum EXIT_STATUS {SUCCESS, FILE_FOUND, INVALID_PRESET_TYPE};

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);

   return EXIT_STATUS->FILE_FOUND;
}
0
lhgarrett
Asked:
lhgarrett
1 Solution
 
emildCommented:
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 Color {RED,GREEN,BLUE,NONE};
enum FontStyle {BOLD,ITALIC,NONE};

since `NONE' is defined twice.
0
 
lhgarrettAuthor Commented:
Excellently explained. Thanks!!!
0

Featured Post

Making Bulk Changes to Active Directory

Watch this video to see how easy it is to make mass changes to Active Directory from an external text file without using complicated scripts.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now