简体   繁体   中英

How do I fix this error? (arduino casting)

I'm using a library called DS3231 by rinkydinkelectronics Link: http://www.rinkydinkelectronics.com/library.php?id=73 (click on manual)

i'm trying to run the following code

String alarmTime = "08:52:00";

        void loop(){
           if (rtc.getTimeStr() == alarmTime){
           alarmState = true;
          }
}

but i get the following error:

exit status 1 no match for 'operator==' (operand types are 'char*' and 'String')

the library manual however says that the return value is a string so I don't see why this shouldn't work :(

Can someone help me fix this or tell me what might be wrong?

Thank you!

If understood your code right, you want to check whether both strings are equal. Because the standard library is not available in the Arduino IDE, you must choose a different way. Convert the C string ( char* ) to a String object .

Example:

if(String(rtc.getTimerStr()) == alarmTime) {
    ....
}

This should work.

You're trying to compare two different things with confusingly similar names. A string (C style string) is a null terminated char array. This is different from the String object . It's generally accepted that with extremely memory limited hardware such as the standard Arduino boards you should avoid the use of the String class if possible as it uses more memory and may cause memory fragmentation from dynamic memory allocation. Much better to use strings instead, which are actually pretty much just as easy to work with as String.

Your code using only strings:

char alarmTime[] = "08:52:00";

void loop() {
  if (strcmp(rtc.getTimeStr(), alarmTime) == 0) {
    alarmState = true;
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM