pic11
pic11

Reputation: 14973

Value initialize aggregate

Tried C++ standard, but couldn't figure it out. Are these equivalent?

double x[2] = {0.0, 0.0};

and

double x[2] = {};

How about these?

struct A {
    double x[2];
};

A a = {0.0, 0.0};

and

A a = {};

Thank you!

Upvotes: 3

Views: 296

Answers (2)

Oliver Charlesworth
Oliver Charlesworth

Reputation: 272667

The C++ standard says (8.5.1):

If there are fewer initializers in the list than there are members in the aggregate, then each member not explicitly initialized shall be value-initialized

and value-initialization of a double is to set it to 0.0.

So yes! In C++ they are equivalent.

[I haven't had time to check the C99 standard.]

Upvotes: 4

littleadv
littleadv

Reputation: 20282

Yes, they are.

Compiler fills the initialization with zeroes when not enough given per declared size.

Upvotes: 3

Related Questions