什么是 ISO C++禁止指针和整数之间的比较?

发布于 2025-01-18 06:53:19 字数 638 浏览 3 评论 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;
          

我写了这些代码 有编译错误

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

说 ISO C ++禁止指针和整数之间的比较 因此,我已经更改了一些更改,例如'ugm3 = 80ug/m3'或'80ug/m3'等。 但是发生同样的错误 该错误是什么意思?


    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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

猫九 2025-01-25 06:53:19

您的 output 变量是一个(单个)char,当用作 < (或几乎任何其他运算符)的操作数时,它会被转换(或“升级”)为 int。但是,您的 "ugm3 = 80ug/m3" 是一个 字符串文字,它被解释为指向字符串第一个元素的指针

正如编译器给出的错误消息(非常清楚地)指出的那样,C++ 中禁止进行整型和指针类型之间的比较。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文