This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Strings and arrays

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": ...

because the compiler returns an error. The switch function seems to not point on arrays


I could declare the strings in an 3D array
U8 au8text[3][3] = {"abc","def","ghi"};
but how to point on this array ?

I've seen on the web a method with structures, but it doesn't work :
#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);
}
The 'if (strcmp (ctp->u8String, u8String) == 0)' never works. I've tried to monitor on hyperterminal the ctp->u8String, but it displays irrelevant characters...



Any suggestion or link to browse ?

Thanks
Stephane

Parents

  • The value in a C case statement has to be an integer constant. So, strings don't qualify. You'll need to do a search through the string table.

    The point of the version with structures is to do just that. It compares a given string with the table, and returns an integer value; you can then use that integer value in a case statement.

    If ctp->u8String looks bad, then that's probably the reason the strcmp() function is failing to find a match. Make sure your table is actually initialized. (Look at the memory locations listed in the map file with your debugger and see what the table looks like.) Note that you'll need to include the INIT.A51 code found in the Keil library directory to actually copy initial values for initialized variables from ROM to RAM.

    Also, since these table values are probably constant, there's perhaps not much reason to actually copy them from ROM to RAM and have them taking up RAM space as well as ROM. If you want them to stay in your code space, you could declare them with the "code" memory qualifier thus:

    static
    struct struct u8CommandString code CommandTable[] =

Reply

  • The value in a C case statement has to be an integer constant. So, strings don't qualify. You'll need to do a search through the string table.

    The point of the version with structures is to do just that. It compares a given string with the table, and returns an integer value; you can then use that integer value in a case statement.

    If ctp->u8String looks bad, then that's probably the reason the strcmp() function is failing to find a match. Make sure your table is actually initialized. (Look at the memory locations listed in the map file with your debugger and see what the table looks like.) Note that you'll need to include the INIT.A51 code found in the Keil library directory to actually copy initial values for initialized variables from ROM to RAM.

    Also, since these table values are probably constant, there's perhaps not much reason to actually copy them from ROM to RAM and have them taking up RAM space as well as ROM. If you want them to stay in your code space, you could declare them with the "code" memory qualifier thus:

    static
    struct struct u8CommandString code CommandTable[] =

Children
  • 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