char* timecompare(){
char time[8];
snprintf(time,8,"%i:%02i",hour(),minute());
return time;
}
char* timefeed = "8:0";
if (strcmp(timecompare(), timefeed) == 0){
Serial.println("hello");
}
I have this as my code when timecompare() and timefeed are both equal it is not printing hello? I this a pointer problem? I instead of comparing timecompare() with timefeed I compare timecompare() with “8:0” then the loop works… Is this a problem with the timefeed variable?
You are returning a stack allocated variable,
time, fromtimecompare(). This is illegal since stack allocated memory is only valid in the function in which the variable is declared.Instead you need to return a heap allocated string. Your compiler should be warning you of this. You could write it like this:
Remember to
free()the memory after you are finished with it.