473,414 Members | 1,674 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

Associating strings with an enum (can I use a macro?)

I use this kind of construct sometimes:
typedef enum color {
red,
orange,
blue
} color;

char *colors_strs[] = { "red", "orange", "blue" };

That way, I can easily print the textual name of enum variables, like:
color foreground = blue;
printf("foreground is %s", colors_strs[ foreground ] );

Maybe there's a more clever way to have my enum's associatiable with a
string (any ideas?) - if not, I'd at least like to automate the
generation of my enum and associated char *array[] using a macro. I'm
using GNU cpp.

I'm imagine its usage would look something like
ENUM_STRING( color, red, orange, blue );
that would produce the enum declaration and the color_strs[] above.

I've fiddled with cpp macros but to no avail. I haven't been able to
produce one that will stringify each arg in a vararg list. The closest
I could get, below, stringifies its entire vararg list (you get "red,
orange, blue" as a single string). I'd like to figure out how to get
"red", "orange", "blue".

#define ENUM_STRING( enm, ...) char * enm ## _str = #__VA_ARGS__;
typedef enum enm { __VA_ARGS__ } enm;
Nov 13 '05 #1
2 3168
tr*********@aol.com (Troy Hanson) writes:
I use this kind of construct sometimes:
typedef enum color {
red,
orange,
blue
} color;

char *colors_strs[] = { "red", "orange", "blue" };

That way, I can easily print the textual name of enum variables, like:
color foreground = blue;
printf("foreground is %s", colors_strs[ foreground ] );

Maybe there's a more clever way to have my enum's associatiable with a
string (any ideas?) - if not, I'd at least like to automate the
generation of my enum and associated char *array[] using a macro. I'm
using GNU cpp.


Something like this (not tested):

#define COLORS \
COLOR(red) \
COLOR(orange) \
COLOR(blue)

#define COLOR(x) x,
typedef enum color {
COLORS
terminator_for_c90 /* may be omitted in C99 */
} color;
#undef COLOR

#define COLOR(x) #x,
char *colors_strs[] = {
COLORS
};
#undef COLOR
--
"This is a wonderful answer.
It's off-topic, it's incorrect, and it doesn't answer the question."
--Richard Heathfield
Nov 13 '05 #2
Ben Pfaff <bl*@cs.stanford.edu> wrote in message news:<87************@pfaff.stanford.edu>...
tr*********@aol.com (Troy Hanson) writes:
I use this kind of construct sometimes:
typedef enum color {
red,
orange,
blue
} color;

char *colors_strs[] = { "red", "orange", "blue" };

That way, I can easily print the textual name of enum variables, like:
color foreground = blue;
printf("foreground is %s", colors_strs[ foreground ] );

Maybe there's a more clever way to have my enum's associatiable with a
string (any ideas?) - if not, I'd at least like to automate the
generation of my enum and associated char *array[] using a macro. I'm
using GNU cpp.


Something like this (not tested):

#define COLORS \
COLOR(red) \
COLOR(orange) \
COLOR(blue)

#define COLOR(x) x,
typedef enum color {
COLORS
terminator_for_c90 /* may be omitted in C99 */
} color;
#undef COLOR

#define COLOR(x) #x,
char *colors_strs[] = {
COLORS
};
#undef COLOR


Ah -- good idea. Thanks for your help.
Nov 13 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Jonathan Mcdougall | last post by:
For use in many of my projects, I implemented an enum with scope using a class containing an enum with operator overloading. Wrapped in a macro, it gives class A { public: scoped_enum(Test)...
6
by: James Brown | last post by:
Hi, I have the following enum declared: enum TOKEN { TOK_ID = 1000, TOK_NUMBER, TOK_STRING, /*lots more here*/ }; What I am trying to do is _also_ represent ASCII values 0-127 as TOKENs...
8
by: Christopher Benson-Manica | last post by:
Given an enumeration something like enum footype { foo=3, bar=5, baz=17, quux=200 }; is there a fast way to access all four of these values? I.e., given
5
by: DJTB | last post by:
Dear Group, I'd like to check if a value is defined in an enum. Example: ------------------------------------------------------ typedef enum { A_VALUE = 1,
3
by: Chris | last post by:
This is what I want to do, I have enum and I want to turn it into a string using the number I've assigned to and concatenting a string to the end of it or displaying some error string for invalid...
7
by: d.kaufmann | last post by:
i have this part of code: typedef enum { OW_SEARCH_ROM = 0xF0, OW_MATCH_ROM = 0x55, OW_CONDITIONAL_SEARCH_ROM = 0xEC, OW_HANDLE_SELECT = 0x0F } ow_command_t;
3
by: kenkahn | last post by:
Given the following simple c program #include <stdio.h> enum ATTRTYPE { CFG_ATTR = 3000 }; int main() { #define xx(id) printf("ATTR=%s\n",#id);
3
by: bg_ie | last post by:
Hi, Lets say I have a TreeView with nodes that represent objects of a number of different types. Is there a way of associating the Set method of each object with its node in the TreeView? That...
10
by: abir | last post by:
Hi, This is not strictly a C++ language question. Is there any way to form a string from an identifier with the preprocessor with some operations ? i.e to say to make a string from an...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.