473,323 Members | 1,551 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,323 software developers and data experts.

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 1609
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: SamMan | last post by:
Is there an easy way to re-initialize an array, other than looping through it's length and setting the values to null, or ""? Would unset() be a wise choice? Thanks again -- SamMan
6
by: steveneng | last post by:
C++ Primer Plus Programming Exercises 4th Ed - Prate Help I'm trying to refresh myself and I'm stuck on this problem (not homework/school related but for personal advancement). 6: Do...
4
by: Mark Hannon | last post by:
I am trying to initialize an array only once so it can be seen & used by any functions that need it. As I understand it, if a variable is declared by itself outside of any functions, its scope is...
2
by: slack_justyb | last post by:
Hello, I'm trying to figure the best way of doing the following. *I need an array of strings that are globally accessable from within 'arrayhere.h' *I need that array of strings to not change...
6
by: Ramprasad A Padmanabhan | last post by:
I have a simple structure defined like this struct userid { char uid; int insize; int outsize; }; typedef struct userid user;
2
by: deko | last post by:
I trying to create a jagged array of two arrays, with the second array being an array of two-dimensional arrays. A graphical representation might look like this: x y y y x y x y y x ...
18
by: toton | last post by:
Hi, In C++ when I initialize an array it, also initializes the class that it contains, which calls the default constructor. However, I want to initialize the array only (i.e reserve the space) and...
15
by: thinktwice | last post by:
char a = { 0 } is it ok?
3
by: vduber6er | last post by:
Lets say I have this structure: typedef struct numbers { double first = 0.0; double second = 0.0; double third = 0.0; } MYVALUES; and I initialize an array of MYVALUES like the following
11
by: Bob Altman | last post by:
Hi all, I have a class that contains a member variable that is an array of class instances: class MyClass { private: SomeClass m_someClass; SomeClass m_arrayOfClasses; };
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.