how to initialise an array data member with all zeros

hi,

is there any other way to do it rather than looping?

similar to this:

int array[100] = { 0 };

thx
memset(array, 0, 100);

You can find the memset() function in <cstring> (or string.h if you are using C rather than C++).

For more details, check out http://www.cplusplus.com/reference/clibrary/cstring/memset.html

I hope this helps! ^_^

rpgfan
wow,

thank you very much... that's a nice trick. I suppose that is only possible in the constructor, and not in an initialiser list, isn't is?

nevertheless, better than a loop...

cheers

ps: what if it is an array of objects, like this data member:

QString _results[NUM_OF_ALGORITHMS];
Last edited on
The default constructor will be executed for each element of the array automatically.
For that matter, int's default constructor runs 100 times in your original example; the reason you have to explicitly initialize the elements to 0 is because int's default constructor does nothing.

ok thx,

now i understand....
Topic archived. No new replies allowed.