cplusplus.com cplusplus.com
cplusplus.com   C++ : Forums : General C++ Programming : how to initialise an array data member w...
  Search:
- -
C++
Information
Documentation
Reference
Articles
Sourcecode
Forums
Forums
Beginners
Windows Programming
UNIX/Linux Programming
General C++ Programming
Articles
Lounge
Jobs

-

question  how to initialise an array data member with all zeros

naja (18)
hi,

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

similar to this:

int array[100] = { 0 };

thx
|
rpgfan3233 (111)
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
|
naja (18)
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
jsmith (158)
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.

|
naja (18)
ok thx,

now i understand....
|

This topic is archived - New replies not allowed.
Home page | Privacy policy
© cplusplus.com, 2000-2008 - All rights reserved - v2.2
Spotted an error? contact us