Here’s what I am trying to do:
typedef enum { ONE, TWO, THREE } Numbers;
I am trying to write a function that would do a switch case similar to the following:
char num_str[10]; int process_numbers_str(Numbers num) { switch(num) { case ONE: case TWO: case THREE: { strcpy(num_str, num); //some way to get the symbolic constant name in here? } break; default: return 0; //no match return 1; }
Instead of defining at every case, is there a way to set it using the enum variable like I am trying to do above?
There’s no built-in solution. The easiest way is with an array of
char*where the enum’s int value indexes to a string containing the descriptive name of that enum. If you have a sparseenum(one that doesn’t start at 0 or has gaps in the numbering) where some of theintmappings are high enough to make an array-based mapping impractical then you could use a hash table instead.