这段代码有什么问题:
#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 逐步执行此函数,它不会通过任何情况,而是在 switch 情况下通过默认值。
我的目标是该缓冲区应该有 RED 而不是它对应的枚举值,即 64。
编译器:Windows XP 上的 gcc 3.4.5