Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

string representation of enum values [duplicate]

Tags:

c

gcc 4.4.2 c89

I have the following enum:

enum drop_options_e
{
    drop_ssm,
    drop_snm,
    drop_ssb
};

I am just wondering that is the best way to get the string representation value from the enum.

So basically, instead of returning the value of 0 for drop_ssm, I could get the 'drop_ssm' instead.

Many thanks for any advice,

like image 580
ant2009 Avatar asked Nov 29 '22 19:11

ant2009


1 Answers

One way is to do like this:

enum drop_options_e
{
    drop_ssm = 0,
    drop_snm ,
    drop_ssb  ,
    LAST_ENTRY   /* Should be last entry */
};

const char* drop_options_s[LAST_ENTRY] = {"drop_ssm", "drop_snm", "drop_ssb"};

when you want a string representation of an enum you can drop_options_s[enum];

like image 66
Naveen Avatar answered Dec 15 '22 15:12

Naveen