Why are these not recognised as being the same?

I expected this program to serialprint "SAME!" but it does not. Why does it not?

char hist[40][14] = {"4444444444444", "1111100000000"
                     , "2222200000000", "3333300000000"
                     , "4444400000000", "0000000000000"
                     , "0000000000000", "8000000000000"
                     , "0000000000000", "1000000000000"
                     , "0000000000000", "0000000000000"
                     , "0000000000000", "0000000000000"
                     , "0000000000000", "0000000000000"
                     , "1230000000000", "0000000000000"
                     , "0000000000000", "0000000000000"
                     , "0000000000000", "0000000000000"
                     , "0000000000000", "0000000000000"
                     , "0000000000000", "8000000000000"
                     , "0000000000000", "1000000000000"
                     , "0000000000000", "0000000000000"
                     , "0000000000000", "0000000000000"
                     , "0000000000000", "0000000000000"
                     , "0000000000000", "0000000000000"
                     , "0000000000000", "0000000000000"
                     , "0000000000000", "0000000000000"
                    };
String bard = "4444444444444";
void setup() {
  // put your setup code here, to run once:

  Serial.begin(9600);
    Serial.println(bard);
  Serial.println(hist[0]);
    if (bard.c_str()==hist[0]){
        Serial.println("SAME!");
    }
}

void loop() {
  // put your main code here, to run repeatedly:
}

Use strcmp() to compare C-strings. Short example:

#define Password_Length 8
char Master[][Password_Length] = {"123A456","1234567","1234568"};

void setup() {
Serial.begin(115200);
for (int i=0; i<3; i++) { //compare passwords
  Serial.print("i = ");
  Serial.print(i);
  Serial.print(" result: ");
  Serial.println(strcmp(Master[i],"123A456"));
}
}
void loop() {}

It is best to avoid using String objects on MCUs, as they cause memory problems and program crashes.

1 Like

bard.c_str() returns a pointer to a char string

and == is a String operation.

You could #include <string.h> and use strcmp() [= string compare] of just pretend that bard is an array of numbers ending in 0, the null terminator of every char string and run a loop to flag false if any 2 elements in either array does not match else the flag stays true.. quit checking on the first false and the compiler will optimize that tight and no need to get into string.h at all.

Also, your Serial speed takes over a millisecond to clear a single char of output. When the 64 char print buffer fills, your code stalls until there is room to get the whole printed line into that buffer.

1 millisecond is 16000 cpu cycles to use or waste......

String variables are not well suited to small memory environments right down to how they are used. Yes you can lock the worst behavior out but by then.. WHY put up with the useless overhead when strings are closer to the metal?

1 Like

The evils of Arduino Strings and how to use strings (null terminated character arrays) in their place.

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.