473,387 Members | 1,497 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,387 software developers and data experts.

Array initialisation

HI

Would i be able to initialize an entire array by:-

int a[10] = 0

if not, how old be able to do it, would i have to use some form of loop
to set each individual element?

Dan

Jul 19 '05 #1
16 10688
John Dibling wrote:
On Mon, 04 Aug 2003 19:15:59 +0100, Danny <da***********@ntlworld.com>
wrote:

HI

Would i be able to initialize an entire array by:-

int a[10] = 0

if not, how old be able to do it, would i have to use some form of loop
to set each individual element?

Dan

The code you have above will initialize the first element; all others
will be uninitialized (eg, garbage). You can do this:

int a[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};


int a[10] = {0};

would be sufficient.

or use a loop as you suggested.


HTH,
--ag
--
Artie Gold -- Austin, Texas

Jul 19 '05 #2
Danny wrote in news:3F**************@ntlworld.com:
HI

Would i be able to initialize an entire array by:-

int a[10] = 0
int a[10] = {};

if not, how old be able to do it, would i have to use some form of loop
to set each individual element?


The above initializer causes all of a's elements to be default
initialized, in the case of an int this means 0, so you get what
you want.

Note that:

int b[3] = { 1 };
is the same as
int b[3] = { 1, 0, 0 };

HTH

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 19 '05 #3
Artie Gold wrote:
...
The code you have above will initialize the first element; all others
will be uninitialized (eg, garbage). You can do this:

int a[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};


int a[10] = {0};

would be sufficient.
...


As Rob noted in his message, even

int a[10] = {};

would be sufficient.

--
Best regards,
Andrey Tarasevich
Brainbench C and C++ Programming MVP

Jul 19 '05 #4
On Mon, 04 Aug 2003 13:27:41 -0700, Andrey Tarasevich
<an**************@hotmail.com> wrote:
Artie Gold wrote:
...
The code you have above will initialize the first element; all others
will be uninitialized (eg, garbage). You can do this:

int a[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};


int a[10] = {0};

would be sufficient.
...


As Rob noted in his message, even

int a[10] = {};

would be sufficient.


I don't think that's correct...

8.5.1.7 :

"If there are fewer initializers in the list than there are memvers in
the aggregate, then each member not explicitly initialized shall be
default initialized."

Doesn't this mean anything not explicitly initialized would be
uninitialized in this case?
</dib>
John Dibling
Witty banter omitted for your protection
Jul 19 '05 #5
"John Dibling" <dib@substitute_my_full_last_name_here.com> wrote...
On Mon, 04 Aug 2003 13:27:41 -0700, Andrey Tarasevich
<an**************@hotmail.com> wrote:
Artie Gold wrote:
...
The code you have above will initialize the first element; all others
will be uninitialized (eg, garbage). You can do this:

int a[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};

int a[10] = {0};

would be sufficient.
...


As Rob noted in his message, even

int a[10] = {};

would be sufficient.


I don't think that's correct...

8.5.1.7 :

"If there are fewer initializers in the list than there are memvers in
the aggregate, then each member not explicitly initialized shall be
default initialized."

Doesn't this mean anything not explicitly initialized would be
uninitialized in this case?


Just scroll a few paragraphs back and read 8.5/5. For a POD to
default-initialise means to zero-initialise. 'int' is a POD.

Victor
Jul 19 '05 #6
John Dibling wrote:
...
As Rob noted in his message, even

int a[10] = {};

would be sufficient.
I don't think that's correct...

8.5.1.7 :

"If there are fewer initializers in the list than there are memvers in
the aggregate, then each member not explicitly initialized shall be
default initialized."


Actually, the above is exactly what makes the '{}' initializer
sufficient in this case. I don't see what in this quote makes you think
that the initialization is incorrect.
Doesn't this mean anything not explicitly initialized would be
uninitialized in this case?


No, it means exactly what it says: everything not explicitly initialized
will be _default_ _initialized_. Default initialization for 'int's means
zero initialization. See a reference in Victor's message. Take a look at
8.5.1/8 as well.

--
Best regards,
Andrey Tarasevich
Brainbench C and C++ Programming MVP

Jul 19 '05 #7
> >> ...
As Rob noted in his message, even

int a[10] = {};

would be sufficient.


I don't think that's correct...

8.5.1.7 :

"If there are fewer initializers in the list than there are memvers in
the aggregate, then each member not explicitly initialized shall be
default initialized."


Actually, the above is exactly what makes the '{}' initializer
sufficient in this case. I don't see what in this quote makes you think
that the initialization is incorrect.
Doesn't this mean anything not explicitly initialized would be
uninitialized in this case?


No, it means exactly what it says: everything not explicitly initialized
will be _default_ _initialized_. Default initialization for 'int's means
zero initialization. See a reference in Victor's message. Take a look at
8.5.1/8 as well.


I noted your quote on a paragraph from some source:

8.5.1.7 :

"If there are fewer initializers in the list than there are memvers in
the aggregate, then each member not explicitly initialized shall be
default initialized."

which I assume to be some online C++ reference. Where can I view this for
myself?
Regards
WD
Jul 19 '05 #8

"Web Developer" <no****@hotmail.com> wrote in message
news:3f********@news.iprimus.com.au...
> ...
>As Rob noted in his message, even
>
> int a[10] = {};
>
>would be sufficient.

I don't think that's correct...

8.5.1.7 :

"If there are fewer initializers in the list than there are memvers in
the aggregate, then each member not explicitly initialized shall be
default initialized."


Actually, the above is exactly what makes the '{}' initializer
sufficient in this case. I don't see what in this quote makes you think
that the initialization is incorrect.
Doesn't this mean anything not explicitly initialized would be
uninitialized in this case?


No, it means exactly what it says: everything not explicitly initialized
will be _default_ _initialized_. Default initialization for 'int's means
zero initialization. See a reference in Victor's message. Take a look at
8.5.1/8 as well.


I noted your quote on a paragraph from some source:

8.5.1.7 :

"If there are fewer initializers in the list than there are memvers in
the aggregate, then each member not explicitly initialized shall be
default initialized."

which I assume to be some online C++ reference. Where can I view this for
myself?

The C++ standard.
http://www.zib.de/benger/C++/clause8.html#s8
Jul 19 '05 #9
Rob Williscroft <rt*@freenet.REMOVE.co.uk> wrote in message news:<Xn**********************************@195.129 .110.130>...
Danny wrote in news:3F**************@ntlworld.com:
HI

Would i be able to initialize an entire array by:-

int a[10] = 0


int a[10] = {};


Thanks for the tip...I'd been using a bunch of loops to accomplish
same.

One question. If I have an array as part of a class, initializing to
zero using empty braces doesn't work. eg

class arrayholder{
int a[100] = {};
};

doesn't work. I have to use:

class arrayholder{
int a[100];
};

arrayholder::arrayholder(){
for(int i = 0; i<100; i++)
a[i] = 0;
}

and initialize it in the constructor. However, I'd like to do
something like:
a[] = {};

in the constructor. any ideas?
Thanks
Jul 19 '05 #10
On Tue, 5 Aug 2003 19:48:17 +0930, "Web Developer"
<no****@hotmail.com> wrote:

which I assume to be some online C++ reference. Where can I view this for
myself?
Regards
WD


This is a reference to the ISO C++ standard document, which describes
the language in detail. AFAIK, there is no onlne location to view
this document; it is copyrighted material, and you must purchase a
copy. Here's one place where you can get one:

http://webstore.ansi.org/ansidocstor...C+14882%2D1998

</dib>
John Dibling
Witty banter omitted for your protection
Jul 19 '05 #11
"J. Campbell" <ma**********@yahoo.com> wrote...
Rob Williscroft <rt*@freenet.REMOVE.co.uk> wrote in message news:<Xn**********************************@195.129 .110.130>...
Danny wrote in news:3F**************@ntlworld.com:
HI

Would i be able to initialize an entire array by:-

int a[10] = 0


int a[10] = {};


Thanks for the tip...I'd been using a bunch of loops to accomplish
same.

One question. If I have an array as part of a class, initializing to
zero using empty braces doesn't work. eg

class arrayholder{
int a[100] = {};
};

doesn't work.


Of course it doesn't. You're not in Java any more. The class
definition can contain only _declarations_ of data members, with
a simple exception: a static const of integral type is allowed
to be initialised there.
I have to use:

class arrayholder{
int a[100];
};

arrayholder::arrayholder(){
for(int i = 0; i<100; i++)
a[i] = 0;
}

and initialize it in the constructor. However, I'd like to do
something like:
a[] = {};

in the constructor. any ideas?


Nope. That's the limitation of the language.

Victor
Jul 19 '05 #12
J. Campbell wrote:
<snip>
One question. If I have an array as part of a class, initializing to
zero using empty braces doesn't work. eg
<snip> However, I'd like to do
something like:
a[] = {};

in the constructor. any ideas?


Perhaps you should consider using a vector, for this and many other
reasons. (See the FAQ for comp.lang.c++ for more reasons.)

In your particular case, your code could look like this:

#include <vector>

class ArrayHolder
{
public:
ArrayHolder()
: d_v(10, 0)
{ /* no code needed for vector initialization */ }
private:
std::vector<int> d_v;
};

The vector constructor I used above will initialize the vector with 10 '0's.

I understand that you need to know how arrays work in C++, as you need
to understand the basics. However, STL containers are usually a better
choice than arrays in C++.

- Adam

Jul 19 '05 #13
J. Campbell wrote in
news:b9**************************@posting.google.c om:
Rob Williscroft <rt*@freenet.REMOVE.co.uk> wrote in message
news:<Xn**********************************@195.129 .110.130>...
Danny wrote in news:3F**************@ntlworld.com:
> HI
>
> Would i be able to initialize an entire array by:-
>
> int a[10] = 0


int a[10] = {};


Thanks for the tip...I'd been using a bunch of loops to accomplish
same.

One question. If I have an array as part of a class, initializing to
zero using empty braces doesn't work. eg

class arrayholder{
int a[100] = {};
};

doesn't work. I have to use:

class arrayholder{
int a[100];
};

arrayholder::arrayholder(){
for(int i = 0; i<100; i++)
a[i] = 0;
}

and initialize it in the constructor. However, I'd like to do
something like:
a[] = {};

struct arrayHolderBase
{
int a[100];
};

class arrayHolder: arrayHolderBase
{

public:

arrayHolder() : arrayHolderBase() {}
};
The important part of the above is: "... : arrayHolderBase() { ..."
bit. This explicit call of the POD's (arrayHolderBase's) default
constructor causes the arrayHolderBase sub object of arrayHolder
to be defualt initialized (all fields/elements set to 0).

Just for fun I compiled the following:

#include <iostream>

struct arrayHolder
{
char a[100];
arrayHolder() : a() {}
};

void test()
{
arrayHolder a;

for (int i = 0; i < 100; ++i)
{
if (a.a[i])
{
std::cerr << "non 0 at " << i << "\n";
return;
}
}
}

void set()
{
arrayHolder a;

for (int i = 0; i < 100; ++i)
{
a.a[i] = i + 20;
}
}

int main()
{
set();
test();
std::cerr << "OK";
}

I fouund the results a bit suprising, No Errors/Warning's
(aka diagnostics) with any of the 3 compilers I tried.

g++ (gcc 3.2 MingW) : OK
msvc (7.1) : non 0 at 0 - OK
bcc32 (borland): non 0 at 0 - OK

I was really expecting (hopeing for) a diagnostic, BTW I used
-W -Wall -pedantic with g++.

I suspect all 3 compilers are conforming implementation's (in this
regard) of our beloved C++ Standard.

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 19 '05 #14
Rob Williscroft wrote:
...
struct arrayHolderBase
{
int a[100];
};

class arrayHolder: arrayHolderBase
{

public:

arrayHolder() : arrayHolderBase() {}
};
The important part of the above is: "... : arrayHolderBase() { ..."
bit. This explicit call of the POD's (arrayHolderBase's) default
constructor causes the arrayHolderBase sub object of arrayHolder
to be defualt initialized (all fields/elements set to 0).
Strictly speaking, in standard terminology the '... : arrayHolderBase()
{ ...' bit is not and explicit call to 'arrayHolderBase' constructor.
The '()' is just a form of member initializer which causes this member
to be default-initialized. No constructors are involved in this process.
Just for fun I compiled the following:

#include <iostream>

struct arrayHolder
{
char a[100];
arrayHolder() : a() {}
};

void test()
{
arrayHolder a;

for (int i = 0; i < 100; ++i)
{
if (a.a[i])
{
std::cerr << "non 0 at " << i << "\n";
return;
}
}
}

void set()
{
arrayHolder a;

for (int i = 0; i < 100; ++i)
{
a.a[i] = i + 20;
}
}

int main()
{
set();
test();
std::cerr << "OK";
}

I fouund the results a bit suprising, No Errors/Warning's
(aka diagnostics) with any of the 3 compilers I tried.
There shouldn't be any. The code is legal.
g++ (gcc 3.2 MingW) : OK
msvc (7.1) : non 0 at 0 - OK
bcc32 (borland): non 0 at 0 - OK

I was really expecting (hopeing for) a diagnostic, BTW I used
-W -Wall -pedantic with g++.

I suspect all 3 compilers are conforming implementation's (in this
regard) of our beloved C++ Standard.


No. Looks like only 'g++' was able to produce the correct result. The
'msvc' and 'bcc32' are not conforming in this respect. As for 'msvc',
this problem existed in version 6 as well.

--
Best regards,
Andrey Tarasevich
Brainbench C and C++ Programming MVP

Jul 19 '05 #15
Andrey Tarasevich wrote in news:3F**************@hotmail.com:
struct arrayHolder
{
char a[100];
arrayHolder() : a() {}
};


I fouund the results a bit suprising, No Errors/Warning's
(aka diagnostics) with any of the 3 compilers I tried.


There shouldn't be any. The code is legal.
g++ (gcc 3.2 MingW) : OK
msvc (7.1) : non 0 at 0 - OK
bcc32 (borland): non 0 at 0 - OK

I was really expecting (hopeing for) a diagnostic, BTW I used
-W -Wall -pedantic with g++.

I suspect all 3 compilers are conforming implementation's (in this
regard) of our beloved C++ Standard.


No. Looks like only 'g++' was able to produce the correct result. The
'msvc' and 'bcc32' are not conforming in this respect. As for 'msvc',
this problem existed in version 6 as well.


Thanks for the correction.

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 19 '05 #16
> > which I assume to be some online C++ reference. Where can I view this
for
myself?
Regards
WD


This is a reference to the ISO C++ standard document, which describes
the language in detail. AFAIK, there is no onlne location to view
this document; it is copyrighted material, and you must purchase a
copy. Here's one place where you can get one:

http://webstore.ansi.org/ansidocstor...FISO%2FIEC+148
82%2D1998

What is the latest version for this document? version 1.0?
WD

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.504 / Virus Database: 302 - Release Date: 24/07/2003
Jul 19 '05 #17

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Christopher Jeris | last post by:
I am relatively new to JavaScript, though not to programming, and I'm having trouble finding the idiomatic JS solution to the following problem. I have a table with (say) fields f1, f2, f3. I...
2
by: Remi Bastide | last post by:
Is is possible to initialize a javascript associative array inline, as you would do in PHP, e.g. : <?php $a = array("abc" => "def", "ghi" => "jkl"); ?>
7
by: Michael | last post by:
Hi all, I have got a simple question about the #define command: Can I use it for array initialisation? E.g. #define ARRAYINIT { {0, 1}, {2,3} }
7
by: Frank M. | last post by:
I'm trying to declare an array of pointers to structures so that I can make the last element a NULL pointer. I figure that it would more easily allow my library routines to know when to stop...
18
by: Vasileios Zografos | last post by:
Hello, can anyone please tell me if there is any difference between the two: double Array1; and
3
by: Louis Caron | last post by:
I am facing the following problem: - I have to build a const array containing functions pointers - the indexes for this array are generated automatically by a tool (not my property) How do I...
3
by: mark.bergman | last post by:
Running lint on code initialising a local 2-D array gives a warning. void f(void) { int a = { 0 }; ... lint gives "warning: Partially elided initialisation..." Should this be happening,...
15
by: jamx | last post by:
How can you initialize an array, in the initialization list of a constructor ?? SomeClass { public: SomeClass() : *init here* { } private: int some_array; };
14
by: dan | last post by:
I would like to have the preprocessor automatically generate the number of array elements requested. Each element is zero. The elements get pasted into a larger array. The other elements may be...
31
by: mdh | last post by:
I am still having a problem understanding K&RII on p 112. I have looked at the FAQs --which I am sure answer it in a way that I have missed, so here goes. A 2-dim array, (per K&R) is really a...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...

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.