Go Down

Topic: Is there a function to compare two const __FlashStringHelper* strings? (Read 2419 times) previous topic - next topic


... or I need to write my own ?
I know this question is trivial, but I did not find any reference ...


Should be doable at compile time ( or even when typing the source text ; )


Did you mean the pointer was constant rather than the string itself ? 


Sep 10, 2012, 02:19 pm Last Edit: Sep 10, 2012, 02:21 pm by brunialti Reason: 1
const __FlashStringHelper* p1;
const __FlashStringHelper* p2;

<compare p1,p2 to check if the two pointed progmem strings are equal>


Even in PROGMEM you should avoid duplicate const strings ( Which is a compile time issue ).
I don't know of an avr-gcc feature to check and optimize duplicate strings, however. 

At runtime, you might maximally have the task to compare the pointers themselves, if they refer to the same loaction in flash.
Or to compare variable strings in RAM with const strings in PROGMEM.


If the strings are in progmem they are same/different at compile time. Why on earth can't you determine if they are the same?

Either I don't understand what your problem is or you don't understand that you don't have one.


I use string as a way to store data structure in progmem. I need to search in progmem the matching subkey to get the remaining infos.
I could manage the data in many different ways, or I could even copy a const __FlashStringHelper* into a string and then compare with the search string ... but i was curious to know if there is a compare function for a couple of const __FlashStringHelper*.
Quite trivial, is'nt it?


I honestly don't know if there is such a function, but I think by now (10:55am to 8:30pm) I could have developed one.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.
I speak for myself, not Arduino.


I did.
But it has nothing to do with the question about a built in function probably faster than mine. If any.
I assume there is not :-)


This compiles, but I'm not sure if it works:
Code: [Select]

const __FlashStringHelper* s1;
const __FlashStringHelper* s2;
const __FlashStringHelper* s3;

int strcmp(const __FlashStringHelper* s1, const __FlashStringHelper* s2);

void setup() {
    s1 = F("same");
    s2 = F("different");
    s3 = F("same");

    if (strcmp(s1, s2) == 0)
        Serial.println("s1, s2 same");
        Serial.println("s1, s2 different");

    if (strcmp(s1, s3) == 0)
        Serial.println("s1, s3 same");
        Serial.println("s1, s3 different");

    if (strcmp(s2, s3) == 0)
        Serial.println("s2, s3 same");
        Serial.println("s2, s3 different");
void loop() {}

int strcmp(const __FlashStringHelper* s1, const __FlashStringHelper* s2) {
    return strcmp_P(reinterpret_cast<const char*>(s1), reinterpret_cast<const char*>(s2));


I am investigating about this.

I'm trying to do something like this:

sprintf(buf,F("<br><br><input type='submit' value='Relay 1' class='c%d' onclick='f(1,%d)'>"),digitalRead(RELAY1),digitalRead(RELAY1));

But I receive this error:

cant convert  '__FlashStringHelper*' to 'const char*' for argument 2 for 'int sprintf(char*, const char*, ...)'

I've found I can do this:

sprintf_P(buf,PSTR("<br><br><input type='submit' value='Relay 1' class='c%d' onclick='f(1,%d)'>"),digitalRead(RELAY1),digitalRead(RELAY1));

But I don't know the difference between F() and PSTR(). Can't find clear documentation about this.

Go Up