C++ 无法打印指向0的字符*
这里的注释2打印得非常完美。其中as注释未打印,且 执行该语句后,程序立即结束。 谁能提供解决方案C++ 无法打印指向0的字符*,c++,C++,这里的注释2打印得非常完美。其中as注释未打印,且 执行该语句后,程序立即结束。 谁能提供解决方案 #include <iostream> int main() { const char * comment = 0; const char * comment2 = "hello this is not empty"; std::cout << std::endl; std::cout << comment2 << std::
#include <iostream>
int main()
{
const char * comment = 0;
const char * comment2 = "hello this is not empty";
std::cout << std::endl;
std::cout << comment2 << std::endl;
std::cout << "printing 0 const char *" << std::endl;
std::cout << comment << std::endl;
std::cout << "SUCCESSFUL" << std::endl;
}
将指针分配给0意味着将其分配给NULL。如果需要字符0,请将其更改为字符串0或空字符串
const char * comment = "";
标准::cout
等于
const char * comment = NULL;
如果要打印字符0,请尝试以下代码:
const char * comment = "0";
当你标记C++时,最好使用< /P>
std::字符串注释0 取消引用空指针是未定义的行为,这会使注释成为空指针:
const char * comment = 0;
如果要将空字符串更改为:
const char* comment = "";
或者使用std::string:
const char*comment=工作正常。但是如果我们把零赋给const char,它就停在那个点上了。在此之后,它不会执行任何语句。我想知道为什么会有这种行为?这是打印以零字节结尾的字符串和打印由零指针指向的内容之间的区别。。。实际上,程序崩溃了
const char* comment = "";
std::string comment;