0

    Serial.print("ugm3 = " );
    Serial.print(ugm3);
    Serial.println("ug/m3" );
 
    lowpulseoccupancy = 0;
    starttime = millis();

    if (Serial.available())                   
    {
      char output = Serial.read();            

      if(output < "ugm3 = 80ug/m3")
      {
        for(int i = 0; i<5; i++)               
        {
          angle = angle +1;
          if(angle >= 30)
          angle = 30;
          

I wrote these code there is compiling error at

if(output < "ugm3 = 80ug/m3")

it says ISO C++ forbids comparison between pointer and integer so, I've already changed some times such as 'ugm3 = 80ug/m3' or '80ug/m3' etc. but the same error occurs what does that error mean?

canton7
  • 37,633
  • 3
  • 64
  • 77
Recovery
  • 1
  • 2
  • `output` is a `char`, `"ugm3 = 80ug/m3"` is `char const [14]`, which converts to `char const *` in the context in which you're using it. A `char` is not a `char*`. – WhozCraig Apr 01 '22 at 09:29
  • Literal strings, like `"ugm3 = 80ug/m3"`, are really arrays of characters. As any other array they will decay to a pointer to their first element. You compare this pointer with a single character, which will be promoted to an `int`. So you're comparing an `int` with an `char*`. Does that make sense to you? – Some programmer dude Apr 01 '22 at 09:30
  • 2
    What are you actually trying to do? What are you really supposed to compare? Please [edit] your question to elaborate. Also please take some time to read [the help pages](http://stackoverflow.com/help), take the SO [tour], read [ask], as well as [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). – Some programmer dude Apr 01 '22 at 09:31
  • Oh I see Thx bros – Recovery Apr 01 '22 at 09:32
  • lol, you can easily cast your pointer to integer (or vice versa) and then it will work, but you have other problems in your code. – user1095108 Apr 01 '22 at 09:34
  • 1
    @user1095108 "Other problems" indeed, like that there's no guarantee that the pointer will even be representable as an `int`. Or that the comparison still won't make any sense. – Some programmer dude Apr 01 '22 at 09:39
  • One feature of C++ is *type safety*. Other languages do a lot of things implicitly. In some languages, `x = 3; y = "14"; if ( x < y ) echo "true";` will actually print "true" without any warnings or errors -- but it also means that the code you wrote might not actually do what you thought it would do, and you will not realize until much later. C++ requires that types *match*, or explicitly be casted by the developer *if you know exactly what you are doing* (and assume you don't!). Consider this a feature, just like other compiler warnings. – DevSolar Apr 01 '22 at 09:40
  • @Someprogrammerdude so what? If he wants to, we should help him do it. But really, by "other problems" I meant, the problems in the code he currently has. He obviously would like to read in a string, but reads a single char, ... never mind the comparison, lol. – user1095108 Apr 01 '22 at 12:42

1 Answers1

0

Your output variable is a (single) char which, when used as the operand of < (or almost any other operator), is converted (or 'promoted') to an int. However, your "ugm3 = 80ug/m3" is a string literal, which is interpreted as a pointer to the first element of the string.

Comparisons between integral types and pointer types are forbidden in C++, as indicated (quite clearly) by the error message your compiler gives you.

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
  • 1
    ... although what you're actually trying to achieve with that comparison is unclear, so I can't really offer an alternative that would work. – Adrian Mole Apr 01 '22 at 09:36