3

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!

2
  • C or C++? You've tagged the question with both. Commented Jun 22, 2011 at 7:50
  • 2
    It's supposed to be A a = { {0.0, 0.0} }; Commented Jun 22, 2011 at 7:52

2 Answers 2

4

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.]

Sign up to request clarification or add additional context in comments.

Comments

3

Yes, they are.

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.