Solved

Writing a function for an enumerated data type

Posted on 1997-12-06
7
191 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
ID: 1256527
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
ID: 1256528
/* 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
ID: 1256529
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
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 
LVL 84

Expert Comment

by:ozo
ID: 1256530
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
ID: 1256531
ok, i'm with you on the split up (.c vs .h).

where to next?

0
 
LVL 84

Expert Comment

by:ozo
ID: 1256532
/* 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
ID: 1256533
Adjusted points to 90
0

Featured Post

New! My Passport Wireless Pro Wi-Fi Mobile Storage

Portable wireless storage to offload, edit, and stream anywhere.

High-capacity, wireless mobile storage designed to accompany professional photographers and videographers in the field to easily offload, edit and stream captured photos and high-definition videos.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
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 recursion in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

895 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

14 Experts available now in Live!

Get 1:1 Help Now