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!

R. Martinho Fernandes
  • 228,013
  • 71
  • 433
  • 510
pic11
  • 14,267
  • 21
  • 83
  • 119

2 Answers2

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

Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680
3

Yes, they are.

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

littleadv
  • 20,100
  • 2
  • 36
  • 50