在C中使用枚举类型的变量作为字符串的简单方法?我想做的是:typedef enum { ONE, TWO, THREE } Numbers;我正在尝试编写一个函数,它将执行类似于以下情况的开关情况:char num_str[10];int process_numbers_str(Numbers num) {
switch(num) {
case ONE:
case TWO:
case THREE:
{
strcpy(num_str, num); //some way to get the symbolic constant name in here?
} break;
default:
return 0; //no match
return 1;}与其在每一种情况下定义,是否有一种方法可以像我前面所做的那样使用枚举变量来设置它呢?
3 回答
郎朗坤
TA贡献1921条经验 获得超9个赞
char*
enum
int
吃鸡游戏
TA贡献1829条经验 获得超7个赞
// Define your enumeration like this (in say numbers.h);ENUM_BEGIN( Numbers ) ENUM(ONE), ENUM(TWO), ENUM(FOUR)ENUM_END( Numbers )// The macros are defined in a more fundamental .h file (say defs.h);#define ENUM_BEGIN(typ) enum typ {#define ENUM(nam) nam#define ENUM_END(typ) };// Now in one and only one .c file, redefine the ENUM macros and reinclude// the numbers.h file to build a string table#undef ENUM_BEGIN#undef ENUM#undef ENUM_END#define ENUM_BEGIN(typ) const char * typ ## _name_table [] = {#define ENUM(nam) #nam#define ENUM_END(typ) };#undef NUMBERS_H_INCLUDED // whatever you need to do to enable reinclusion#include "numbers.h"// Now you can do exactly what you want to do, with no retyping, and for any// number of enumerated types defined with the ENUM macro family// Your code follows;char num_str[10];int process_numbers_str(Numbers num) { switch(num) { case ONE: case TWO: case THREE: { strcpy(num_str, Numbers_name_table[num]); // eg TWO -> "TWO" } break; default: return 0; //no match return 1;}// Sweet no ? After being frustrated by this for years, I finally came up// with this solution for my most recent project and plan to reuse the idea// forever
- 3 回答
- 0 关注
- 1049 浏览
添加回答
举报
0/150
提交
取消