Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
如果我创建动态字符数组:
char * c = new char[5];
而且我没有用字符串填充它,空字符\0会在这个数组的末尾吗?
\0
不,char 指针只是指针。他们甚至不必指向数组。
在您的特定情况下,新数组的元素未初始化。如果您希望它们被零初始化,您可以这样做:
char * c = new char[5]();
这会将所有元素设置为\0. 您可以考虑c指向一个以空字符结尾的字符串。
c