6

I want to initialize all struct members to 0. Common solution is to make something like this:

struct foo bar = {0}

I create this example:

#include <stdio.h>

struct Stru2 {
    int c;
    int d;
};

struct Stru1 {
    int a;
    Stru2 b;
};

int main()
{
    struct Stru1 aaa = { 0 };
    return aaa.b.c;
}

And I compile (gcc 4.6.3) it whit this parameters, to make sure how ANSI handle this

gcc -Wall -Wextra -pedantic -ansi main.cpp

And I got following warnings:

main.cpp: In function ‘int main()’: 
main.cpp:36:28: warning: missing initializer for member ‘Stru1::b’ [-Wmissing-field-initializers]

The question is, why -Wextra, generate this warning? Maybe not always "= {0}", set all members to 0?

Thomas Dickey
  • 51,086
  • 7
  • 70
  • 105
NiegodziwyBeru
  • 864
  • 1
  • 10
  • 19

3 Answers3

12

It's just a nanny warning.. = {0} definitely sets all of the members to zero. The reason it's included as a warning at all is for the case where you might add a field to a structure and you want to make sure it gets initialized correctly. Having the warning lets you find those places quickly and fix them. For example, let's say you fix have an initializer something like:

struct Stru1 aaa = { 1, { 2, 3 } };

Now, add a field int e to Stru2:

struct Stru2 {
    int c;
    int d;
    int e;
};

And let's pretend it's very important that e get initialized. Well, without -Wmissing-field-initializers, you'd have to hunt around in your code to find every place you care about. With the warning, you get a diagnostic message like the one you saw, and you can find and fix them very easily:

struct Stru1 aaa = { 1, { 2, 3, 4 } };

To get the advantage of this warning, you do have to go through and fully initialize all of your structures, though. In your case, that means something like:

struct Stru1 aaa = { 0, { 0, 0 } };
Carl Norum
  • 219,201
  • 40
  • 422
  • 469
  • 1
    `-Wextra` has other "nanny" warnings in addition to this one. Sometimes they're annoying, but other times the author of the code can be quite thankful for them. – David Hammen Nov 05 '12 at 19:18
  • +1 @DavidHammen. I also like it detecting accidental use of `=` rather than `==` in conditionals. I don't know if that's one of the `-Wextra` family or not, but it's a nice one to have. – Carl Norum Nov 05 '12 at 22:50
  • That's a `-Wparentheses` warning, which is enabled by `-Wall`. – David Hammen Nov 05 '12 at 23:36
5

Yes, {0} sets all struct members to 0, because it sets the first member to 0, and all other members are by default set to 0 as long as at least one member is initialized. See Are uninitialized struct members always set to zero?.

Community
  • 1
  • 1
1''
  • 26,823
  • 32
  • 143
  • 200
3

This is related to GCC bug #53119, --Wmissing-braces wrongly warns about universal zero initializer {0}, but I think your question indicates that there may be at least one other warning option adding to the problem. Posting follow-ups to the bug thread would probably help to get the issue addressed.

R.. GitHub STOP HELPING ICE
  • 208,859
  • 35
  • 376
  • 711