Solved

Writing a function for an enumerated data type

Posted on 1997-12-06
7
190 Views
Last Modified: 2010-04-02
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
0
Comment
Question by:luonght
  • 3
  • 3
7 Comments
 
LVL 1

Accepted Solution

by:
terrycj earned 90 total points
Comment Utility
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
 
LVL 84

Expert Comment

by:ozo
Comment Utility
/* 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
 
LVL 1

Expert Comment

by:terrycj
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 84

Expert Comment

by:ozo
Comment Utility
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
 
LVL 1

Expert Comment

by:terrycj
Comment Utility
ok, i'm with you on the split up (.c vs .h).

where to next?

0
 
LVL 84

Expert Comment

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

0
 

Author Comment

by:luonght
Comment Utility
Adjusted points to 90
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now