tags:

views:

227

answers:

6

Suppose there is a structure such as:

struct XYZ
{
double a;
double b;
}

Now we make an object of it

struct XYZ abcd[10];

and now I am filling this array.

I have to initialize this array because I call this in a loop and it overwrites the previous values.

If it overwrites the previous 5 values by 4 new values then still the 5th one is still there in the array which results in the wrong output.

+3  A: 

If you use c++ you can define a constructor, observe:

struct XYX {
   double a, b;

   XYX() { a=0; b=0; }
}
Cem Kalyoncu
or `XYX() : a(0), b(0) {}`
Mark
-1 What about the initialization field?
fnieto
mark, make your comment an answer to be accepted.
fnieto
I find XYX() { a=0; b=0; } clearer and novice friendly. I use initializer list when I must.
Cem Kalyoncu
Initializer list is only necessary, when creating and assigning for the field is expensive. Doubles are not expensive, I see no need to downvote this.
SadSido
I find `XYX() : a(0), b(0) {}` to do exactly what needs to be done: initialization, not assignment. I use assignment when I must (i.e., in case of dependencies between member variables when I don't want to depend on declaration order). Yes, it doesn't really make any difference with `double`, but this is the same as with `++i` vs. `i++`: do the popular, but sloppy `i++` all the time, relying on the compiler to optimize it, and you're pretty sure to forget about doing the _right_ `++i` when `i` is an expensive iterator and it matters. Do it always right, then it's always right.
sbi
To be pedantic up to the end, I suggest using XYZ() : a(0.0), b(0.0) {} not to mistreat integer zeros.
SadSido
@SadSido: Actually we've all been wrong. `:)` I have no idea why nobody came up with this, but the one and only way to write that ctor is this: `XYX() : a(), b() {}`. It will initialize `a` and `b` right whatever their type is.
sbi
+1  A: 
for (int i=0; i<10; ++i)
{
    abcd[i].a = 0.0;
    abcd[i].b = 0.0;
}

Of course, if some of the slots haven't been filled with meaningful data you probably shouldn't be looking at them in the first place.

Andrew Medico
Fine for C, but ugly and unstylish for C++.
piotr
+1  A: 

If your question is how to initialize the array elements then @cemkalyoncu answer will help you.

If it over rites the previous 5 values by 4 new values then still the 5th one is still there in the array which in result gives wrong output.

For this case it is better you go for vector. You can remove the unwanted elements from the vector to make sure that it does not contain the wrong values.

In this case, remove 5th element from vector if you no longer use.

aJ
A: 

Also you can initialize with memset function

memset(&abcd[index], 0, sizeof(XYZ));
Arash
-1 what if the structure has a string? this is ugly, C-ish and prone to error.
fnieto
I wouldnt use it unless I need to initialize a well known structure array containing many 100 elements.
Cem Kalyoncu
Note that there are architectures where all-bits-set-to-zero is not a valid `double` value.
sbi
yes you right if structure has string, but in this case with double number memset is work fine
Arash
@Arash: Have you actually read my comment before you put yours underneath???
sbi
+3  A: 

Initializing a struct is easily done by enumerating it's member values inside curly braces. Beware, though, 'cause if a member is omitted from the enumeration, it will be regarded as zero.

struct A { int a_, b_; };

A a = { 1, 2 };
A b = { 1 }; // will result in a_=1 and b_=0

A as [] = { {1,2}, {1,3}, {2,5} };

Strangely enough, this also works for the public members of an "aggregate" class. It's to be discouraged, though, since this way your class will lose it's ability to do the necessary things at construction time, effectively reducing it to a struct.

xtofl
The ability to use brace-initialization is one of the biggest reasons for choosing to make a class an aggregate -- see boost::array, for example.This is a design choice; Not something to be "discouraged".(Of course, in C++0x we get std::initializer_list and can make "proper" classes that are also brace-initializable.)
me22
A: 

In addition to xtofl's answer, note, that if you want to zero-initialize the array, all you have to do is write

XYZ abcd[10] = {};
avakar