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

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

Children
No data