Hello people, My question is C langage use. I'd like to define a list of strings and to compare this list with a string incoming from the UART in order to build a command line interpreter. The string from the UART is saved in a buffer. Let's name it au8TextBuffer. The problem is that I can't do a
switch (au8TextBuffer) case "abc": ... case "def": ...
U8 au8text[3][3] = {"abc","def","ghi"};
#define CMD_TBL_LEN (sizeof (cmd_tbl) / sizeof (cmd_tbl [0])) enum { CMDID_HELP, CMDID_SETVAR, CMDID_ENDLIST }; struct u8CommandString { unsigned char *u8String; unsigned char u8Identifier; }; static struct u8CommandString CommandTable[] = { { "HELP", CMDID_HELP}, { "SV", CMDID_SETVAR}, }; U8 Cmdid_Search (U8 *u8String) { struct u8CommandString *ctp; for (ctp = CommandTable; ctp < &CommandTable[CMD_TBL_LEN]; ctp++) { if (strcmp (ctp->u8String, u8String) == 0) { UART_Printf("reussi !!!!!"); return (ctp->u8Identifier); } } return (CMDID_ENDLIST); }
Some more suggestions... > U8 au8text[3][3] = {"abc","def","ghi"}; These strings are exactly 3 chars long, so they will be NOT zero terminated and strcmp() will NOT work on them. Instead use zero-terminated strings or use strncmp(). The *code* attribute suggested by Drew Davis, is required because otherwice it will be placed in RAM which is a waste... but even so, data variables should get initialized. May be something is wrong with your startup code. --P