Dan Rahenkamp
Dan Rahenkamp

Reputation: 3

Compare a date pointer with a date

In an Arduino script how do I compare a date pointer with a string that is a date. Currently I am trying:

while(year(t)=="1970") {  
    getTime();
   }

but I am getting a can't compare a pointer with a string compiler error which I understand but I would like to compare the two somehow and the somehow is where I am stuck. Thanks for any help for this newbie

Upvotes: 0

Views: 96

Answers (3)

Azaraks
Azaraks

Reputation: 26

if(year(t)==1970)
{
    getTime();
}

year() returns 4-digit year integer. not a string.

Upvotes: 1

Alberto
Alberto

Reputation: 4282

I would import <string.h> Then use strcmp() to compare the two strings:

In your case

if (strcmp(string1,string2) == 0) {
  //Some good stuff :)
}

C does not support direct comparison between strings. This because strings are char arrays and should be manipulated properly

Usually, a for loop is required to make a proper comparison, but in this case it is much easy using a library function, that does the exact same thing

Just to let it know, to compare you would use something like

for(i=0;s[i]!='\0';i++) {
  //Loop till end of string
  //Check if every char of string 1 is equal to the one in the same position of string 2
}

Hope this was helpful.

Best regards.

Upvotes: 0

Gopi
Gopi

Reputation: 19864

Comparing strings should be done using strcmp() not with == operator

You are actually comparing the pointers and not the strings by using ==

Upvotes: 1

Related Questions