30

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 following warning from gcc :

warning: missing braces around initializer

What am i doing wrong - is there another/better way to do this ?

Jon
  • 428,835
  • 81
  • 738
  • 806
TCSGrad
  • 11,898
  • 14
  • 49
  • 70
  • 1
    Does this help you? http://stackoverflow.com/questions/201101/how-to-initialize-an-array-in-c (see the second answer) – rlb.usa Mar 25 '11 at 15:54
  • @RedX - STRUCTA is a random example...any structure throws this - I can't reveal the exact structure as it company code :) – TCSGrad Mar 25 '11 at 16:02

7 Answers7

37

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.

AnT stands with Russia
  • 312,472
  • 42
  • 525
  • 765
  • Cannot disable the warning....company policy, and I'd have to say it results in a cleaner code !! – TCSGrad Mar 25 '11 at 16:03
  • 14
    @shan23: I'd say that the cleanest code is `{ 0 }`, which is the universal zero initializer in C. Your company policy is destroying one of the cleanest idioms of C language (and a very useful one). – AnT stands with Russia Mar 25 '11 at 16:05
  • 3
    +1 I mostly agree, I would say it's not company policy but gcc policy that's the problem. `-Wno-braces` actually also disables other *potentially useful* brace-related warnings (like `if if else` stuff), and gcc is simply wrong and stupid not to consider the universal zero initializer special in this regard. – R.. GitHub STOP HELPING ICE Mar 25 '11 at 18:50
9

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

pmg
  • 106,608
  • 13
  • 126
  • 198
5

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.

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

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

Pablo Alvarez
  • 154
  • 1
  • 5
1

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.

nmichaels
  • 49,466
  • 12
  • 107
  • 135
1

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;
}
Rumple Stiltskin
  • 9,597
  • 1
  • 20
  • 25
0

Can STRUCTA be assigned to 0?

You can always use memset(), too.

tamarintech
  • 1,972
  • 12
  • 17
  • See the example from Rumple Stiltskin above. That's what I was trying to communicate. If STRUCTA cannot be directly assigned to {0} then gcc won't let you do it. memset blindly will, though. – tamarintech Mar 25 '11 at 16:06
  • 1
    Firstly, it is not assignment, it is *intialization*. Secondly, *any* struct (or array) in C language can be initialized with `{ 0 }`. – AnT stands with Russia Mar 25 '11 at 16:09
  • ... something like mystruct x { int a; int b; } can be initialized to {0} ? I thought, at minimum, you would need {{0, 0}} – tamarintech Mar 25 '11 at 16:21
  • @AndreyT: Not just structs and arrays. **Any type** in C can be initialized with `{0}`. `int x = {0};` is valid C. – R.. GitHub STOP HELPING ICE Mar 25 '11 at 17:58