Solved

Writing a function for an enumerated data type

Posted on 1997-12-06
7
194 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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

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…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

809 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