Compare progmem string

Hi,

How can I check if a PROGMEM string is equal to other string?

Despite both strings Str1 and Str2 print “Test” they are not equal …my code:

#include <avr/pgmspace.h>
prog_char txt_0 PROGMEM = “Test”;

PROGMEM const char *txt_table ={
txt_0
};

char *Str2=“Test”;

void setup()
{
Serial.begin(9600);
}

void loop()
{
char buffer[30];
char Str1=strcpy_P(buffer, (char)pgm_read_word(&(txt_table[0])));
if(Str1==Str2){
Serial.println(Str1);
}
delay( 500 );
}

strcmp_P

if(Str1==Str2){

Here, you're comparing two string pointers. Only if they both point to the same string will they have the same value.

could you be more explicit..i've made some tries but I dind't get there :(

http://www.nongnu.org/avr-libc/user-manual/group__avr__pgmspace.html

The first result of a single-term Google search.

If I do this Serial.println( strcmp_P(Str2, Str1) ); the output is 5, shouldn’t be 0 if they are equal?

I can't see your code...

#include <avr/pgmspace.h>
prog_char txt_0 PROGMEM = “Test”;

PROGMEM const char *txt_table ={ txt_0};

char *Str2=“Test”;

void setup()
{
Serial.begin(9600);
}

void loop()
{
char buffer[30];
char Str1=strcpy_P(buffer, (char)pgm_read_word(&(txt_table[0])));

Serial.println( strcmp_P(Str2, Str1) );

delay( 500 );
}

After copying the string from PROGMEM, just use strcmp, not strcmp_P.

That’s it, thanks.

Another question, is it possible to do something like this:

PROGMEM const char *txt_table[0] =txt_0;
PROGMEM const char *txt_table[1] =txt_1;

I would like to create a loop to fill the txt_table.

PaulS:
After copying the string from PROGMEM, just use strcmp, not strcmp_P.

Another question, is it possible to do something like this:

PROGMEM const char *txt_table[0] =txt_0; PROGMEM const char *txt_table[1] =txt_1;

This doesn't make sense. Fill the table as it is declared, or size it correctly, and fill it one element at a time.

Either: PROGMEM const char *txt_table[] = {txt_0, txt_1};

or: PROGMEM const char *txt_table[2]; txt_table[0] = txt_0; txt_table[1] = txt_1;

PROGMEM const char *txt_table[0] =txt_0;
PROGMEM const char *txt_table[1] =txt_1;

No, you can't have two variables with the same name at the same scope.

PROGMEM const char *txt_table[] ={txt_0, txt_1};

PROGMEM const char *txt_table[2]; txt_table[0] = txt_0; txt_table[1] = txt_1;

this doesn't work

error: expected constuctor, destructor, or type conversion before '=' token

PaulS:

Another question, is it possible to do something like this:

PROGMEM const char *txt_table[0] =txt_0; PROGMEM const char *txt_table[1] =txt_1;

This doesn't make sense. Fill the table as it is declared, or size it correctly, and fill it one element at a time.

Either: PROGMEM const char *txt_table[] = {txt_0, txt_1};

or: PROGMEM const char *txt_table[2]; txt_table[0] = txt_0; txt_table[1] = txt_1;

txt_table[0] = txt_0;
txt_table[1] = txt_1;

this doesn't work

That's executable code outside of a function

PROGMEM const char *txt_table[] ={txt_0, txt_1};

is that the only way to fill this array?

Yes; you can't do it at run-time, because it is flash memory.

thanks, I will do as you say.

I’m starting to make a menu, could you tell me if this make any sense.
I will use “parent_id” to relate sub-menus with menus and “action” to choose what function to call

#include <avr/pgmspace.h>
prog_char txt_0 PROGMEM = “Menu 1”; prog_char parent_id_0 PROGMEM = “0”; prog_char action_0 PROGMEM = “ActionMenu1”;
prog_char txt_1 PROGMEM = “Menu 2”; prog_char parent_id_1 PROGMEM = “0”; prog_char action_1 PROGMEM = “”;
prog_char txt_2 PROGMEM = “Menu 2.1”; prog_char parent_id_2 PROGMEM = “1”; prog_char action_2 PROGMEM = “ActionMenu2.1”;
prog_char txt_3 PROGMEM = “Menu 2.2”; prog_char parent_id_3 PROGMEM = “1”; prog_char action_3 PROGMEM = “ActionMenu2.2”;
prog_char txt_4 PROGMEM = “Go Back”; prog_char parent_id_4 PROGMEM = “1”; prog_char action_4 PROGMEM = “Back”;
prog_char txt_5 PROGMEM = “Menu 3”; prog_char parent_id_5 PROGMEM = “0”; prog_char action_5 PROGMEM = “ActionMenu3”;

PROGMEM const char *txt_table ={ txt_0, txt_1, txt_2, txt_3, txt_4, txt_5 };
PROGMEM const char *parent_id_table ={ parent_id_0, parent_id_1, parent_id_2, parent_id_3, parent_id_4, parent_id_5 };
PROGMEM const char *actions_table ={ action_0, action_1, action_2, action_3, action_4, action_5 };

You could probably save yourself some typing by defining a "struct" for those items, assuming they are all associated.

AWOL: You could probably save yourself some typing by defining a "struct" for those items, assuming they are all associated.

Forgive my ignorance, but what do you mean by defining a "struct" ?

A "struct" is a useful way of grouping related items of different types: http://en.wikipedia.org/wiki/Struct_%28C_programming_language%29

Can I use a struct with PROGMEM ? The only benefict will be save me some typing?

Can you give me an example of a struct for my menu, or is it too much to ask?

Thank you for all.