Quickest way to initialize an array of structures to all-0's?

后端 未结 7 610
无人及你
无人及你 2020-12-14 16:49

I\'m trying to initialize an array of structures to all-0\'s, using the below syntax:

STRUCTA array[MAX] = {0};

However, I\'m getting the f

相关标签:
7条回答
  • 2020-12-14 17:23

    It the first member of your struct has a scalar type, use

    STRUCTA array[MAX] = {{ 0 }};
    

    If the first member of your struct happens to be another struct object, whose first member has scalar type, then you'll have to use

    STRUCTA array[MAX] = {{{ 0 }}};
    

    and so on. Basically, you have to open a new level of nested {} every time you "enter" another nested aggregate (a struct or an array). So in this case as long as the first member of each nested aggregate is also an aggregate, you need to go deeper with {}.

    All these extra braces are only there to avoid the warning. Of course, this is just a harmless warning (in this specific case). You can use a simple { 0 } and it will work.

    Probably the the best way to deal with this is to disable this warning entirely (see @pmg's answer for the right command-line option). Someone working on GCC wasn't thinking clearly. I mean, I understand the value of that warning (and it can indeed be very useful), but breaking the functionality of { 0 } is unacceptable. { 0 } should have been given special treatment.

    0 讨论(0)
  • 2020-12-14 17:34

    It depends on the STRUCTA. For example :

    typedef struct structa 
    {
        int a, b;
    } STRUCTA;
    
    int main (int argc, char const* argv[])
    {
        STRUCTA array[10] = {{0,0}};
        return 0;
    }
    
    0 讨论(0)
  • 2020-12-14 17:37

    Arrays are initialized with braces, but so are structs. You probably need to put an extra set of braces around your 0 and, depending on how STRUCTA is defined, some extra 0s separated by commas.

    0 讨论(0)
  • 2020-12-14 17:40

    gcc is being a nuisance. It should accept that without warnings.
    Try this

    STRUCTA array[MAX] = {{0}};
    

    That gcc behaviour can be controlled with the option -Wmissing-braces or -Wno-missing-braces.

    -Wall enables this warning; to have -Wall but not the missing braces, use -Wall -Wno-missing-braces

    0 讨论(0)
  • 2020-12-14 17:45

    This is merely a harmful warning issued by gcc, and I would disable it with -Wno-braces. {0} is an extremely useful "universal zero initializer" for types whose definition your code is not supposed to be aware of, and gcc's discouraging its use is actively harmful to the pursuit of good code.

    If gcc wants to keep this warning, it should at least special-case {0} and disable the warning in this one case.

    0 讨论(0)
  • 2020-12-14 17:45

    You can avoid the warning by using completely empty braces:

    STRUCTA array[10] = {};

    The array will be aggregate-initialized, which means that each of the structs in it will in turn be value-initialized. Value-initialization with empty brackets turns into aggregate-initializion of each struct, which sets all fields to 0, which is what you want.

    This works in all cases as long as your structs are POD (see the links above for precise description).

    0 讨论(0)
提交回复
热议问题