33

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 ?

2
  • 1
    Does this help you? stackoverflow.com/questions/201101/… (see the second answer) Commented Mar 25, 2011 at 15:54
  • @RedX - STRUCTA is a random example...any structure throws this - I can't reveal the exact structure as it company code :) Commented Mar 25, 2011 at 16:02

7 Answers 7

39

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Cannot disable the warning....company policy, and I'd have to say it results in a cleaner code !!
@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).
+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.
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

Comments

6

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.

Comments

3

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

4 Comments

Suggestion: please fill in the answer here on Stack Overflow instead of relying on an external site for details. Also, since this is filed as a "C" question, the reference to PODs is confusing. I suggest changing the last sentence to say, "This always works in C. In C++ your structs must be PODs".
The links in the answer are specific to C++, but this is a C question. To the best of my knowledge, this type of initialization does behave the same way in both languages, but the wording of the rules differs, and, at one point, even the terminology did.
@AdrianMcCarthy -- empty braces initializers were a syntax violation in C until the recent C23 Standard was released, which is to say also that they were a syntax violation when this answer was written (which may explain the downvotes on this answer). But some C compilers, e.g., GCC, did support empty braces initializers long before C23.
does this also work for structs that contain a typedef ? using STRUCTA_TYPEDEF structa = {0} produces "error: invalid conversion from ‘int’ to ‘type’", using {} as suggested has no warning or error -- but are typedefs getting set to zero ?
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.

Comments

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;
}

Comments

-1

Can STRUCTA be assigned to 0?

You can always use memset(), too.

5 Comments

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.
Firstly, it is not assignment, it is intialization. Secondly, any struct (or array) in C language can be initialized with { 0 }.
... something like mystruct x { int a; int b; } can be initialized to {0} ? I thought, at minimum, you would need {{0, 0}}
@AndreyT: Not just structs and arrays. Any type in C can be initialized with {0}. int x = {0}; is valid C.
memset() might not be appropriate - for example if the struct contains pointer or float members.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.