How can I automatically get the first character of a constant string as a constant character?

Is it possible to rewrite the following code, so I only need to change in one place if the line changes?

#define MY_STRING "Foo bar" #define MY_STRING_FIRST_CHAR 'F' 

The following is not valid because it refers to char in a memory location, so it cannot be used as an example in a switch :

 #define MY_STRING_FIRST_CHAR MY_STRING[0] switch (something) { case MY_STRING_FIRST_CHAR: break; } 

The goal is to efficiently analyze the resulting string by looking at one character. In my case, all lines have one unique character. The following is not my actual code, but a very simple example to show the principle:

 #define COMMAND_LIST "list" #define COMMAND_LIST_FIRST_CHAR 'l' #define COMMAND_CHANGE "change" #define COMMAND_CHANGE_FIRST_CHAR 'c' #define COMMAND_EXIT "exit" #define COMMAND_EXIT_FIRST_CHAR 'e' switch(received_command_string[0]){ case COMMAND_LIST_FIRST_CHAR: // Do the "list" stuff break; case COMMAND_CHANGE_FIRST_CHAR: // Do the "change" stuff break; case COMMAND_EXIT_FIRST_CHAR: // Do the "exit" stuff break; } 

User "pmg" found this in the gcc documentation: "Unable to convert macro argument to character constant".

I wanted the definitions to be in an include file that can be used by several source files. This is so close that I can get, but only each character defined in one place:

 #include <stdio.h> #define CH0 'F' #define CH1 'o' #define CH2 'o' #define CH3 ' ' #define CH4 'b' #define CH5 'a' #define CH6 'r' static char MY_STRING[] = { CH0, CH1, CH2, CH3, CH4, CH5, CH6, '\0'}; #define MY_STRING_FIRST_CHAR CH0 void main(void){ printf("The string is %s, the first char is %c\n", MY_STRING, MY_STRING_FIRST_CHAR); } 

I will not do it. The initial question was whether one definition could be used to get both a string constant and a character constant. Due to the loss of clock cycles at run time, there are several solutions to my problem.

+5
source share
3 answers

You can do this by writing each character once ... but in different definitions

 #include <stdio.h> #define COMMAND_LIST_FIRST_CHAR 'l' #define COMMAND_LIST (char[]){ COMMAND_LIST_FIRST_CHAR, 'i', 's', 't', 0 } int main(void) { char received_command_string[] = "list"; switch (received_command_string[0]) { case COMMAND_LIST_FIRST_CHAR: printf("Doing the \"list\" stuff for '%s'\n", COMMAND_LIST); break; default: break; } return 0; } 
+3
source

Why do you absolutely want to use a switch enclosure?

Instead, you can use a mapping table that matches your row and processing functions. Then you just need to iterate over the table.

 typedef struct { char * key; void (func*)(void); } MAP_ENTRY; MAP_ENTRY map [] = { {"list", listHandler}, {"change", changeHandler}, {"exit", exitHandler}, }; for (i = 0; i < sizeof(map)/sizeof(map[0]); i++) { if (map[i].key[0] == received_command_string[0]) { map[i].func(); break; } } 

Then you just need to move the processing code from your / case switch to the corresponding handler function

+1
source
 #define MY_STRING "Hello" const char var[] = MY_STRING; switch(var[0]) { case 'H': break; case 'A': break; } 

This should solve the problem.

-4
source

All Articles