如何初始化一个char数组? [英] how to initialize a char array?
问题描述
char * msg = new char[65546];
要对所有的用户初始化为0。在C ++中最好的做法是什么?
want to initialize to 0 for all of them. what is the best way to do this in C++?
推荐答案
char * msg = new char[65546]();
它被称为值初始化,并在C ++ 03中引入。如果您碰巧在过去十年中陷入困境,那么您需要使用 std :: fill()
(或
It's known as value-initialisation, and was introduced in C++03. If you happen to find yourself trapped in a previous decade, then you'll need to use std::fill()
(or memset()
if you want to pretend it's C).
请注意,这不适用于除了以下之外的任何值:零。我认为C ++ 0x将提供一种方法来做到这一点,但我有点背后的时间,所以我不能评论。
Note that this won't work for any value other than zero. I think C++0x will offer a way to do that, but I'm a bit behind the times so I can't comment on that.
更新:it似乎我对语言的过去和未来的反省不完全准确;请参阅更正的注释。
UPDATE: it seems my ruminations on the past and future of the language aren't entirely accurate; see the comments for corrections.
这篇关于如何初始化一个char数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!