Enumerations in the switch housing

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, /* 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"); } } 

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

+4
source share
3 answers

I just wrote the following program, compiled it and tested it, and the output:

 $ ./test d e $ 

which is to be expected. Hope this helps you identify some of the differences in your program.

 #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); } 
+4
source

In MapFormatToString try printing the value inside the format:

printf("%x", format);

If you don't get 64 (0x64, that is), it means that something went wrong between the destination in the format and reading it inside MapFormatToString . For example, if an enumeration is considered as a 32-bit integer, something might happen to it when converting uint8. Also, first try not to transfer the buffer, just print the format value.

0
source

I copied - pasted your code. Just made a small change to the function call. This gives me the result as desired.

Edit: instead of buffer, transfer buffer

 //MapFormattToString(myformat,&buffer); MapFormattToString(myformat, buffer); 

Here is the main function for your reference:

 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); } 

The compiler is used: gcc (Ubuntu / Linaro 4.6.3-1ubuntu5) 4.6.3

0
source

Source: https://habr.com/ru/post/1414901/


All Articles