How to change an enum type variable to a string type variable?
Enum Types
Collapse
X
-
An enum is an integer value. You just need to change an integer into a string. Use the itoa function in <stdlib.h>. -
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:
The following lookup table mistake does not provoke a compiler warning:Code:const struct { enum Color value; const char *name; } table[] = { ... { RED, "RED" }, ... };
Code:{ RED, "Yellow" },Comment
Comment