无法打印指向 0 的 char*

2024-01-07

这里 comment2 被完美打印。其中注释未打印,并且 该语句一执行,程序就结束。 有人可以提供解决方案吗?

#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;
}

取消引用空指针是未定义的行为,这使得comment空指针:

const char * comment = 0;

如果你想要一个空字符串更改为:

const char* comment = "";

Or use std::string:

std::string comment;
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

无法打印指向 0 的 char* 的相关文章

随机推荐