469,950 Members | 1,868 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,950 developers. It's quick & easy.

How to initialize an array

Hi all,

Here's a really basic C++ syntax question: How do I initialize an array,
whose size is a literal, to all zeros in its initializer without coding a
loop to do it? For example:

#define N 5
bool myData[N];

// Don't want to do this:
if (!m_initDone) {
for (int i = 0; i < N; i++) myData[i] = false;
m_initDone = true;
}

TIA - Bob
Aug 4 '08 #1
5 1459
Bob Altman wrote:
Here's a really basic C++ syntax question: How do I initialize an array,
whose size is a literal, to all zeros in its initializer without coding a
loop to do it? For example:

#define N 5
bool myData[N];
bool myData[N] = {0};

Initialize part, initialize the whole. Works for structs, too.

--
J.
Aug 4 '08 #2
bool myData[N] = {0};
>
Initialize part, initialize the whole. Works for structs, too.
Thanks Jeroen.
Aug 5 '08 #3


"Bob Altman" <rd*@nospam.nospamwrote in message
news:eW**************@TK2MSFTNGP02.phx.gbl...
> bool myData[N] = {0};

Initialize part, initialize the whole. Works for structs, too.

Thanks Jeroen.
You may well already be aware of this, but while the initialization
statement above does set all elements to 0, a statement such as bool
myData[N] = {1}; sets only the first element to 1, and the rest to 0. IOW, a
single initialization value occupies the first element and the compiler
initializes elements for which no explicit value was provided to 0;

Aug 5 '08 #4
On Aug 5, 2:09*am, Jeroen Mostert <jmost...@xs4all.nlwrote:
* *bool myData[N] = {0};

Initialize part, initialize the whole. Works for structs, too.
Actually, there's no need to explicitly initialize any part of it,
even; you can just do:

bool myData[N] = {};
Aug 5 '08 #5
PvdG42 wrote:
>

"Bob Altman" <rd*@nospam.nospamwrote in message
news:eW**************@TK2MSFTNGP02.phx.gbl...
>> bool myData[N] = {0};

Initialize part, initialize the whole. Works for structs, too.

Thanks Jeroen.
You may well already be aware of this, but while the initialization
statement above does set all elements to 0, a statement such as bool
myData[N] = {1}; sets only the first element to 1, and the rest to 0.
IOW, a single initialization value occupies the first element and the
compiler initializes elements for which no explicit value was provided
to 0;
Good point, I should have mentioned that. In fact, when using it to
initialize structs whose first member holds the size of the struct, you use
this explicitly:

Struct struct = {sizeof struct};

--
J.
Aug 5 '08 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by SamMan | last post: by
6 posts views Thread by steveneng | last post: by
4 posts views Thread by Mark Hannon | last post: by
2 posts views Thread by slack_justyb | last post: by
6 posts views Thread by Ramprasad A Padmanabhan | last post: by
2 posts views Thread by deko | last post: by
18 posts views Thread by toton | last post: by
15 posts views Thread by thinktwice | last post: by
11 posts views Thread by Bob Altman | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.