if语句或比较C++

if statement or comparison C++

本文关键字:C++ 比较 语句 if      更新时间:2024-05-10

我是C++新手,但我面临比较问题

void print(int x, char const*b=""){
std::cout << x;
if(b == "a"){
/*this code not execute I don't know why ?*/
std::cout << "n";
}
}
/*calling my print function*/
print(2020, "a")

您的检查正在验证指针的地址空字符串在内存中有一个地址,因此检查不会返回true,因此不会执行iftrue分支。

如果您使用的是C++,您可以考虑将参数更改为使用类型std::string,并使用std::string::empty进行检查。