What is wrong with this piece of code:
#define str(x) #x #define xstr(x) str(x) typedef unsigned char uint8_t; typedef enum { RED = 0x64, GREEN = 0x65, BLUE = 0x87 } Format; char buffer[50]; myformat = RED; MapFormattToString(myformat,&buffer); void MapFormattToString(uint8_t format,char *buffer) { printf("format = %x\n",format); 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"); } }
If I go through this function using myformat = RED, it will not fail in any of the cases, but instead will go by default in the case of a switch.
My goal is for this buffer to have a RED in it, and not the corresponding enum ie 64 value.
Compiler: gcc 3.4.5 on Windows XP
source share