Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/127.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 关于&&;操作人员_C++_Arduino_Embedded_Logical Operators - Fatal编程技术网

C++ 关于&&;操作人员

C++ 关于&&;操作人员,c++,arduino,embedded,logical-operators,C++,Arduino,Embedded,Logical Operators,下面的代码,我一直有奇怪的问题,是为了修剪掉整数数组中未使用的部分,然后将其转换成字符串 例: \u ABC\u DE将成为\u ABC\u DE 当输入用默认字符填充时,问题就会出现。(“在示例中为“u”) sLength是整数数组的长度chars 问题代码: int inputLength = sLength - 1; while (chars[inputLength] == defaultChar && inputLength >= 0) { inp

下面的代码,我一直有奇怪的问题,是为了修剪掉整数数组中未使用的部分,然后将其转换成字符串

例:
\u ABC\u DE
将成为
\u ABC\u DE

当输入用默认字符填充时,问题就会出现。(“在示例中为“u”)

sLength
是整数数组的长度
chars

问题代码:

  int inputLength = sLength - 1;

  while (chars[inputLength] == defaultChar && inputLength >= 0) {
    inputLength--;
  }

  inputLength++;

  Serial.println("input length: " + String(inputLength));
  // (in)sanity check
  Serial.println(inputLength);
  Serial.println(String(inputLength));
  Serial.println(inputLength <= 0);
  Serial.println(0 <= 0);
  Serial.println(inputLength == 0);
  Serial.println(0 == 0);

  if (inputLength <= 0) {
    //reset cursor position
    Serial.println("index set to 0");
    index = 0;
  } else {
    output = "";
    for (int i = 0; i < inputLength; i++) {
      char c = charSet[chars[i]];
      if (c == '_') {
        c = ' ';
      }
      output += c;
    }
    done = true;
  }

如果我的解释正确,那么输出意味着偶数行上的0>0和0=/=0,但是0
&&
是不可交换的。它首先计算左操作数,如果左操作数的计算结果为
0
,则停止计算


您的原始代码失败,因为在某个点它计算
chars[-1]
(这会导致if
chars
是一个数组)。替代版本没有这个问题,因为它在使用
inputLength
作为数组索引之前执行
=0
测试。

&&
是可交换的,因为
a&&b
的结果与
b&&a
的结果相同。但是内置操作符
&&
有一个。这意味着,如果可以通过单独计算第一个操作数来确定
a和&b
的结果,则不计算第二个操作数

因此,当第一个操作数为
chars[inputLength]==defaultChar
inputLength
-1
时,您将进入未定义行为的区域,这意味着程序的行为是不可预测的。但是,在解决方法中,由于
inputLength>=0
inputLength<0
检查,您可以避免未定义的行为,因此代码可以按预期工作


正如@PeteBecker所指出的:
a()&&b()
如果
a()
b()
有副作用,那么
a()&&b()
是不可交换的。

你可以提到,如果
a()
b()
有副作用,那么
a()&&b()
是不可交换的。@PeteBecker:谢谢。在答案中添加了这一点。
input length: 0
0
0
0
1
0
1
  while (chars[inputLength] == defaultChar && inputLength >= 0) {
    inputLength--;
  }
  while (inputLength >= 0 && chars[inputLength] == defaultChar) {
    inputLength--;
  }
  while (chars[inputLength] == defaultChar) {
    inputLength--;
    if (inputLength < 0) {
      break;
    }
  }
input length: 0
0
0
1
1
1
1
index set to 0