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

Popular posts from this blog

objective c - Change font of selected text in UITextView -

php - Accessing POST data in Facebook cavas app -

c# - Getting control value when switching a view as part of a multiview -