【发布时间】:2016-03-03 22:43:39
【问题描述】:
基于: What is the proper function for comparing two C-style strings?
我知道 OP 是正确的。但是,我使用 g++ 4.1.2 尝试了以下操作。
#include <iostream>
using namespace std;
int main()
{
const char* str1 = "hello worldA";
if (str1 == "hello worldr")
std::cout << "hello world" << std::endl;
else if ( str1 == "hello worldA" )
std::cout << "hello worldA " << std::endl;
else
std::cout << "not " << std::endl;
return 0;
}
或
#include <iostream>
using namespace std;
int main()
{
const char* str1 = "hello worldA";
const char* str2 = "hello worldA";
if (str1 == str2)
std::cout << "hello world" << std::endl;
else
std::cout << "not " << std::endl;
return 0;
}
输出显示正确的比较结果。
问题>如何设计程序以显示== 不适用于const char*?
谢谢
【问题讨论】:
-
"我如何设计一个程序以显示 == 不适用于 const char" -- 很简单。如果你知道
const char *不应该被比较,你就不要写代码来做这件事。
标签: c++