Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 ?

like image 495
TCSGrad Avatar asked Mar 25 '11 15:03

TCSGrad


People also ask

How do you initialize an array to all zeros?

Using Initializer List. int arr[] = { 1, 1, 1, 1, 1 }; The array will be initialized to 0 if we provide the empty initializer list or just specify 0 in the initializer list.

How do you initialize an array of structures?

If you have multiple fields in your struct (for example, an int age ), you can initialize all of them at once using the following: my_data data[] = { [3]. name = "Mike", [2]. age = 40, [1].

What is a good way to initialize all the elements of a large array with the same value?

Initializer List: To initialize an array in C with the same value, the naive way is to provide an initializer list. We use this with small arrays. int num[5] = {1, 1, 1, 1, 1}; This will initialize the num array with value 1 at all index.

How do you initialize an array with all elements?

There are two ways to specify initializers for arrays: With C89-style initializers, array elements must be initialized in subscript order. Using designated initializers, which allow you to specify the values of the subscript elements to be initialized, array elements can be initialized in any order.


2 Answers

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.

like image 70
AnT Avatar answered Oct 02 '22 21:10

AnT


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

like image 30
pmg Avatar answered Oct 02 '22 23:10

pmg