WCHAR wszFoo [CONSTANT_BAR] = {0}; <- What does {0} mean?

 WCHAR wszFoo[CONSTANT_BAR] = {0}; 

I have never seen something like {0} used in C ++ as part of a language. And I have no idea how to look for a question like this online. What is it?

+4
source share
4 answers

$ 8.5.1 / 7 -

"If there are fewer initializers in the initializers than there are members in the aggregate, then each member is not explicitly initialized with value-initialized (8.5)."

All this means that there is an explication request to initialize the first element to 0. Since initializers are not specified for the remaining elements, they are initialized with a value. This is in the case of WCHARs initialized to 0.

What does value initialization mean? Here's what the standard at $ 8.5 says (italics mine)

To initialize an object of type T means:

- if T is a class type (section 9) with a constructor declared by the user (12.1), then the default constructor for T is called (and initialization is poorly formed if T has no default constructor available);

- if T is a non-unit type of a class without a constructor declared by the user, then each non-static data element and component of the base class T Value is initialized;

- if T is an array of type, then each element of Value is initialized;

- otherwise the object is initialized to zero <------ WCHAR will do here

For zero initialization of an object of type T means:

- if T is a scalar type (3.9), the value 0 (zero) is converted for the object, converted to T; <------ WCHAR will match here

- if T is not a union type of class, each non-static data element and each subobject of the base class is zeroinitialized;

- if T is a union type, objects first named data member89) is initialized to zero;

- if T is an array type, each element zero is initialized;

- if T is a reference type, no initialization is performed.

+3
source

See array initialization.

Missing initialization values ​​use zero

If the size of the explicit array is equal to the specified, but a shorter initialization list is specified, unspecified elements are set to zero.

float pressure[10] = {2.101, 2.32, 1.44};

This not only initializes the first three values, but all the rest for the elements are set to 0.0. To initialize an array for all zeros, initialize only the first value.

+6
source

Initializes an array.

 float p1[1000]; // No intitialization. float p2[1000] = {0.0}; // All 1000 values initialized to zero. 

More details here: C ++ Notes: Array initialization

+3
source

This means that all wszFoo elements are zero.

+2
source

All Articles