gpt4 book ai didi

c++ - linux c/c++ - 奇怪的 if/else 问题

转载 作者:太空狗 更新时间:2023-10-29 23:34:11 24 4
gpt4 key购买 nike

我正在查询一个 mysql 表,然后循环遍历结果。

其中一个字段的值为“0”,所以当我尝试以下操作时它不起作用!

while ((row2 = mysql_fetch_row(resultset2)) != NULL) {
if (row2[2] != "0") {
// the field has a value of 0, but it's still executing the code here!
} else {
// should be executing this code
}
}

我知道 C/C++ 在涉及变量时非常严格(取消链接 php),但我无法弄清楚这一点。任何人有任何想法为什么?

最佳答案

你正在比较 row2[2],一个指向 char 的指针,和一个指向常量 char 数组 的指针” 0"

使用strcmp(row2[2], "0") != 0(C方案),std::string(row2[2]) != "0"(C++ 解决方案),或者 atoi(row2[2]) != 0 如果您知道 row2[2] 始终是整数的字符串表示形式(并且不能是一个 SQL NULL 值)。

关于c++ - linux c/c++ - 奇怪的 if/else 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5408733/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com