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 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.
bool myData[N] = {0};
>
Initialize part, initialize the whole. Works for structs, too.
Thanks Jeroen.
"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;
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] = {};
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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
|
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...
|
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...
|
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...
|
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;
|
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 ...
|
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...
|
by: thinktwice |
last post by:
char a = { 0 }
is it ok?
|
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
|
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;
};
|
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...
|
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...
|
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...
|
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...
|
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)...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
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....
|
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
|
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...
| |