C 开关情况下的枚举
这段代码有什么问题:C 开关情况下的枚举,c,enums,C,Enums,这段代码有什么问题: #define str(x) #x #define xstr(x) str(x) typedef unsigned char uint8_t; typedef enum { RED = 0x64, GREEN = 0x65, /* other enum values */ BLUE = 0x87 } Format; char buffer[50]; /* other code and
#define str(x) #x
#define xstr(x) str(x)
typedef unsigned char uint8_t;
typedef enum
{
RED = 0x64,
GREEN = 0x65,
/* other enum values */
BLUE = 0x87
} Format;
char buffer[50];
/* other code and variables */
/* somewhere later in code */
myformat = RED;
/* later calling format function */
MapFormattToString(myformat,&buffer);
void MapFormattToString(uint8_t format,char *buffer)
{
printf("format = %x\n",format); /*format printf has output 64 */
switch(format)
{
case RED:
sprintf(buffer,"%s\n", xstr(RED));
break;
case GREEN:
sprintf(buffer,"%s\n", xstr(GREEN));
break;
case BLUE:
sprintf(buffer,"%s\n", xstr(BLUE));
break;
default:
sprintf(buffer,"Unsupported color\n");
}
}
如果我在myformat=RED的情况下单步执行此函数,则它不会通过任何情况,而是通过开关情况下的默认值。我的目标是使缓冲区中应该有红色,而不是对应的枚举值,即64
编译器:在Windows XP上的gcc 3.4.5我刚刚编写了以下程序,对其进行了编译和测试,结果是:
$ ./test
d
e
$
这正是你所期望的。希望这能帮助你发现程序中的一些差异
#include<stdio.h>
typedef unsigned char uint8_t;
typedef enum {
RED = 0x64,
GREEN = 0x65,
BLUE = 0x87
} Format;
void MapFormatToString(uint8_t format, char *buffer) {
switch (format) {
case RED:
sprintf(buffer, "%c\n", RED);
break;
case GREEN:
sprintf(buffer, "%c\n", GREEN);
break;
case BLUE:
sprintf(buffer, "%c\n", BLUE);
break;
default:
sprintf(buffer, "Unknown\n");
}
}
main (int argc, char *argv[]) {
char buffer[100];
MapFormatToString(RED, buffer);
printf(buffer);
MapFormatToString(GREEN, buffer);
printf(buffer);
MapFormatToString(BLUE, buffer);
printf(buffer);
}
#包括
typedef无符号字符uint8;
类型定义枚举{
红色=0x64,
绿色=0x65,
蓝色=0x87
}格式;
void MapFormatToString(uint8_t格式,字符*缓冲区){
开关(格式){
红色表壳:
sprintf(缓冲区,“%c\n”,红色);
打破
绿色外壳:
sprintf(缓冲区,“%c\n”,绿色);
打破
蓝色表壳:
sprintf(缓冲区,“%c\n”,蓝色);
打破
违约:
sprintf(缓冲区,“未知”);
}
}
main(int argc,char*argv[]){
字符缓冲区[100];
MapFormatToString(红色,缓冲区);
printf(缓冲区);
MapFormatToString(绿色,缓冲区);
printf(缓冲区);
MapFormatToString(蓝色,缓冲区);
printf(缓冲区);
}
在MapFormatToString
中尝试打印格式内的值:
printf(“%x”,格式)代码>
如果您没有得到64(即0x64),则意味着要格式化的赋值与在MapFormatToString
中读取它之间出现了错误。例如,如果枚举被视为32位整数,则在转换uint8时可能会发生某些情况。
另外,开始不要传递缓冲区,只打印format的值。我复制了粘贴的代码。只是对函数调用做了一个小改动。它给我想要的输出
更改:传递缓冲区而不是&buffer
//MapFormattToString(myformat,&buffer);
MapFormattToString(myformat, buffer);
以下是供您参考的主要功能:
int main()
{
char buffer[50];
/* other code and variables */
/* somewhere later in code */
Format myformat = BLUE;
/* later calling format function */
//MapFormattToString(myformat,&buffer);
MapFormattToString(myformat, buffer);
// MapFormattToString(0x64, buffer);
printf("\n***** Buffer = %s\n", buffer);
}
使用的编译器:gcc(Ubuntu/Linaro 4.6.3-1ubuntu5)4.6.3尝试将函数参数类型更改为枚举格式。枚举不保证是uint8,有些实现定义为\uu int8\t
。请尝试添加printf(“%08x%08x\n”,格式,A)代码>至MapFormatToString
。它将准确地向您显示所比较的内容。我想其中一个不会是你所期望的。缓冲区是如何定义的&buffer
表示有错误。@Banthar:No,&buffer
是指向字符数组的指针。该值应与buffer
相同,但类型不同。我在问题中添加了更多信息。好的,我按照您的指示修改了我的C程序,并在此处进行了更改——结果完全相同。你能不能制作一个像我一样的小C程序,编译并运行它,看看是否能得到同样的结果?也许您遇到了一些奇怪的编译器错误。我正在使用gcc编译我的。