• C

Writing a function for an enumerated data type

I define an enumerated data type for 4 colors as follows:

enum colors {white, blue, cyan, green};

How do I write a function that accepts a parameter of the above type. The function must print out the name of the color.

Many thanks.

Luong
luonghtAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

terrycjCommented:
You can't get the names of the enums so you can print them.

You're going to need to do something like this

char *colors[] = {
  "white",
  "blue",
  "cyan",
  "green"
};

enum colors {white = 0, blue, cyan, green};

which is a pain because you have your names (and their order)
in 2 places. this solution makes the code you'll write
simple though.

there is nothing to stop you declaring your own
struct and putting a value in there along with
a char * name. this is also a pain because you'll
get bloated code to do such a simple thing.

that's all i'll say. one thing is for sure - the names
you give to your enums are not available to you for
printing. the compiler doesn't put them anywhere
(unless you use -g and your debugger knows about them) and
the C language gives you no way to get at them, etc.

you need another solution, and there are several more
or less awkward ones to choose from :-)

terry.

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ozoCommented:
/* here's a trick I sometimes use: */
#define table\
        x(white,=0)\
        x(blue,)\
        x(cyan,)\
        x(green,)\
        y\
/*end table*/
#define x(color,value)       color value,
#define y       num_colors
enum colors{
        table
};
#undef x
#undef y
#define x(color,value)       #color,
#define y
char *colors[]={
        table
};
main(int argc,char *argv[]){
        enum colors n;
        n = atoi(argv[1]);
        if( white <= n && n < num_colors ){ printf("%s\n",colors[n]); }
}

0
terrycjCommented:
ozo: yes, i have too, but i don't like it very much.

my main trouble with this is that it creates both the enum
definition and the colors array declaration in the same file.
for any project of a reasonable size, you'd want the colors
array to be in the file (say) print_colors.c and the enum to
be in a .h file that was included by lots of .c files.

also, i'd take out the =0 in the enum spec
and use n>0 not white <= n
in the checking since that doesn't rely on
white being first named. it does rely on the first element
in the enum having value 0 (as it will have, unless explicitly
given some other value). this is far less likely to be changed
by some unsuspecting well-intentioned later programmer.

nits, i know.

i solve things like this with make and m4 (or perl or awk etc.).
then i get to have a simple file containing only (e.g.)

color(white)
color(blue)
color(cyan)
color(green)

and i can do anything i want - including writing the checking
function, etc.

but i'm sure that's more machinery than luonght would want to
wheel in...

terry.


0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

ozoCommented:
For a large project, I'd put the
#define table
in a .h. Each .c can then construct whatever they need from it.
I agree that I should have compared n with 0 instead of white before indexing.
And using Perl to generate a C program works well too.
0
terrycjCommented:
ok, i'm with you on the split up (.c vs .h).

where to next?

0
ozoCommented:
/* you could also build something like: */
#define x(color)      if( n == color ){ puts(#color); }
void printcolor(enum color n){
#table
}

0
luonghtAuthor Commented:
Adjusted points to 90
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.