-2

Possible Duplicate:
how does array[100] = {0} set the entire array to 0?
how to initialize a char array?

In c++, I want to initialize char array to 0s array.
Will "char a[4096] = {0};" do this?

Community
  • 1
  • 1
Al2O3
  • 3,103
  • 4
  • 26
  • 52

2 Answers2

2

Yes it will... but keep in mind that a char being 0 is ASCII NULL... if you want it initialized to all '0' (character 0's) that won't work.

In this case:

memset(a, '0', 10);

Would be a better way to go... or

std::fill(std::begin(a), std::end(a), '0');

Would be better yet.

Mike
  • 47,263
  • 29
  • 113
  • 177
  • 2
    `std::fill(std::begin(a), std::end(a), '0');` would be even better. (notice how it *always* means "fill the whole array with zeros" and not "fill N elements of the array with zeros"; reminds me of Perl's "do what I mean" motto) – R. Martinho Fernandes Dec 03 '12 at 14:20
  • @R.MartinhoFernandes - Good one, coming from a C background I'm not always aware of the nice standard methods like this. I've updated the answer to include your comment. Thanks for the input! – Mike Dec 03 '12 at 14:29
0

Yes.

(And this answer has at least 30 characters as well.)