Enum Types

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Ongeziwe
    New Member
    • Aug 2014
    • 1

    Enum Types

    How to change an enum type variable to a string type variable?
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    An enum is an integer value. You just need to change an integer into a string. Use the itoa function in <stdlib.h>.

    Comment

    • donbock
      Recognized Expert Top Contributor
      • Mar 2008
      • 2427

      #3
      Are you seeking to change the integer enum value into a string that matches the name assigned to that value in the enum definition?

      The C compiler does not provide any way to do that. You, however, have the option of creating and managing your own lookup table that associates a string with each value. It will be up to you to make sure that the strings in the table actually match the names in the enum definition. For instance, something like this:
      Code:
      const struct {
         enum Color value;
         const char *name;
      } table[] = {
          ...
          { RED, "RED" },
          ...
      };
      The following lookup table mistake does not provoke a compiler warning:
      Code:
          { RED, "Yellow" },

      Comment

      Working...