c – 对于三元操作,“警告:操作……可能未定义” – 如果/ else
发布时间:2020-12-16 06:51:08 所属栏目:百科 来源:网络整理
导读:参见英文答案 Undefined behavior and sequence points????????????????????????????????????5个 这是我的代码: int main() { static int test = 0; const int anotherInt = 1; test = anotherInt test ? test++ : 0; if (anotherInt test) test++; else te
参见英文答案 >
Undefined behavior and sequence points????????????????????????????????????5个
这是我的代码: int main() { static int test = 0; const int anotherInt = 1; test = anotherInt > test ? test++ : 0; if (anotherInt > test) test++; else test = 0; return 0; } 这是我构建它时产生的警告: ../main.cpp:15:40: warning: operation on ‘test’ may be undefined [-Wsequence-point] test= anotherInt>test ? test++ : 0; ^ 为什么C会对三元操作发出警告,而不是常规if..else语句? 解决方法
他们并不等同.请注意,在三元运算符表达式中,您将结果分配给test
将if条件更改为: if(anotherInt > test) test = test++; // undefined! 您可能也会在此处看到相同的警告. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |