Reputation: 1947
I am using a C-style structure which does not have any constructor, like this:
struct structName {
int mem1;
int mem2;
char mem3;
char mem4;
}
I am creating a variable of this structure and I want to initialize all members of the structure to zero. I found the following methods.
struct structName structVar = {};
struct structName structVar = {0};
struct structName structVar = struct structName();
For the first two methods, my compiler is giving "missing initializer for member" warning.
The third approach compiles without warnings.
Upvotes: 0
Views: 207
Reputation: 75668
The preferred method should be one of:
structName structVar{};
structName structVar = {};
auto structName = structVar{};
there are subtle differences, but not for aggregates as in your example
This has the added advantage that it initializes structVar
for any type of structName
or if it cannot perform an initialization it makes the program ill-formed (the code doesn't compile) (plus it doesn't allow narrowing).
In your specific example, structName
is an agregate:
§8.5.1 Aggregates [dcl.init.aggr]
(1) An aggregate is an array or a class (Clause 9) with no user-provided constructors (12.1), no private or protected non-static data members (Clause 11), no base classes (Clause 10), and no virtual functions (10.3)
The initialization syntax I used is called List-initialization:
§8.5.4 List-initialization [dcl.init.list]
(1) List-initialization is initialization of an object or reference from a braced-init-list. [...] An initializer list may be empty. [...]
For our aggregate this means:
§8.5.1 Aggregates [dcl.init.aggr]
(2) When an aggregate is initialized by an initializer list, as specified in 8.5.4, the elements of the initializer list are taken as initializers for the members of the aggregate, in increasing subscript or member order. Each member is copy-initialized from the corresponding initializer-clause
(7) If there are fewer initializer-clauses in the list than there are members in the aggregate, then each member not explicitly initialized shall be initialized from its brace-or-equal-initializer or, if there is no brace-or-equalinitializer, from an empty initializer list (8.5.4).
[ Example:
struct S { int a; const char* b; int c; int d = b[a]; }; S ss = { 1, "asdf" };
initializes ss.a with 1, ss.b with "asdf", ss.c with the value of an expression of the form int{} (that is, 0), and ss.d with the value of ss.b[ss.a] (that is, ’s’)
[...]
end example ]
So all of these are valid and do the exact same thing:
structName structVar = {};
structName structVar = {0};
structName structVar = {0, 0};
However if there is at least one initializer-clauses
and less than there are members in the aggregate, gcc
and clang
emit a warning. It might be that you intended to initialize all members, but missed some. So the empty initializer list is the safest choice.
As a side note struct
is not needed and universally not used in a declaration. So replace this:
struct structName structVar
with:
structName structVar
Upvotes: 1