c - Strcmp comparing to identical strings but not entering loop -
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 code when timecompare() , timefeed both equal not printing hello? pointer problem? instead of comparing timecompare() timefeed compare timecompare() "8:0" loop works... problem timefeed variable?
you returning stack allocated variable, time
, timecompare()
. illegal since stack allocated memory valid in function in variable declared.
instead need return heap allocated string. compiler should warning of this. write this:
char* timecompare(){ char* time = malloc(8); snprintf(time,8,"%i:%02i",hour(),minute()); return time; }
remember free()
memory after finished it.
Comments
Post a Comment