trouble initialising array

N

Nick Keighley

Hi,

is this correct:-

typedef struct
{
long frequency;
} Structure;

Structure my_struct [32] = {0};

specifically the initialisation of the struct. I get warnings about
missing braces. Should there be two sets of braces?
 
D

Dave Vandervies

Hi,

is this correct:-

typedef struct
{
long frequency;
} Structure;

Structure my_struct [32] = {0};

specifically the initialisation of the struct. I get warnings about
missing braces. Should there be two sets of braces?

This initialization is correct and sensible, and warnings about use of
"{0}" as an initializer for aggregate types should be ignored.

If you were attempting to initialize with nonzero values, you'd want to
use more braces for clarity and resistance to changes; but a single zero
will explicitly initialize the first member of the first struct in the
array and implicitly zero-initialize the rest, which is exactly what you
want, and the compiler isn't clever enough to recognize the difference
between the all-zero and the not-all-zero cases.


dave
 
U

user923005

Hi,

is this correct:-

typedef struct
{
long frequency;
} Structure;

Structure my_struct [32] = {0};

specifically the initialisation of the struct. I get warnings about
missing braces. Should there be two sets of braces?
From ©ISO/IEC ISO/IEC 9899:1999 (E)
§6.7.8 Language page 127:
21 If there are fewer initializers in a brace-enclosed list than there
are elements or members of an aggregate, or fewer characters in a
string literal used to initialize an array of known size than there
are elements in the array, the remainder of the aggregate shall be
initialized implicitly the same as objects that have static storage
duration.
 
N

Nick Keighley

is this correct:-
typedef struct
{
long frequency;
} Structure;
Structure my_struct [32] = {0};
specifically the initialisation of the struct. I get warnings about
missing braces. Should there be two sets of braces?

This initialization is correct and sensible, and warnings about use of
"{0}" as an initializer for aggregate types should be ignored.

note that I have an *array* of structs. Does that make a difference.
This part of my c-code is generated and a warning in there is a little
irritating.
 
P

pete

Nick said:
note that I have an *array* of structs. Does that make a difference.

It makes no difference.
{0} is the universal initializer.
Objects of any and all types may be initialized with {0}.

Initializing an array with a single {0},
is the same as initializing each and every element with {0}.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top