0

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

  • are you asking how to de-reference a pointer? like this `year(*t)` – Hogan Mar 04 '15 at 16:09
  • you obviously are also new to programming in general - it is _very_ advisable to learn programming basics first - without that knowledge you will probably never achieve your goals. The above snippet will loop forever, effectively deadlocking the microprocessor. – specializt Mar 04 '15 at 16:10
  • Arduino code is not written in C. – unwind Mar 04 '15 at 16:13
  • `year()` and `getTime()` are not part of the standard Arduino API. You should state which libraries you are using and, more importantly, what you are trying to accomplish. – Edgar Bonet Mar 04 '15 at 16:15
  • @unwind horribly wrong, every _sketch_ is a c program, being compiled by gcc into hex-code - you get a IDE for C together with very powerful and comfortable libraries. – specializt Mar 04 '15 at 16:17
  • 1
    @specializt: it’s actually C++. – Edgar Bonet Mar 04 '15 at 16:21
  • How is `year()` defined? – alk Mar 04 '15 at 17:22

3 Answers3

1

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

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

Gopi
  • 19,784
  • 4
  • 24
  • 36
1
if(year(t)==1970)
{
    getTime();
}

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

Azaraks
  • 26
  • 3
0

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.

Alberto
  • 4,212
  • 5
  • 22
  • 36