Initializers for arrays of structures

J

John Devereux

Hi,

gcc has started warning about the lack of inner braces in initializers
like :-


struct io_descriptor
{
int number;
char* description;
};


struct io_descriptor io[64] =
{
100, "Start",
101 ,"Reject",
....


Is this definitely illegal C code that I should fix, or is it just a
warning I can turn off?

Thanks,
 
E

Eric Sosman

John said:
Hi,

gcc has started warning about the lack of inner braces in initializers
like :-


struct io_descriptor
{
int number;
char* description;
};


struct io_descriptor io[64] =
{
100, "Start",
101 ,"Reject",
...


Is this definitely illegal C code that I should fix, or is it just a
warning I can turn off?

It is legal C, but the "fully braced" style

struct io_descriptor io[64] = {
{ 100, "Start" },
{ 101, "Reject" },
...,
};

is often preferable, since the compiler has more information
about the intended layout and may be able to issue better
diagnostics if there's something wrong.

In this particular case there's little chance of making
a mistake that wouldn't be caught immediately: Almost all
valid initializers for `int' are invalid for `char*' and
vice versa, so if you get out of step the compiler is very
likely to detect it. Full braces give little advantage
here, and you may decide they're not worth the bother --
although with a decent editor the bother is pretty small.
If you choose to use only the outermost braces, the details
of how to suppress a warning (if it's possible, and if
there's any warning to begin with) should be described in
the documentation. Most gcc installations have an "info"
program; give it a whirl.
 
M

Mark McIntyre

Hi,

gcc has started warning about the lack of inner braces in initializers

(snip example of missing braces)
Is this definitely illegal C code that I should fix, or is it just a
warning I can turn off?

AFAIK you've always needed the braces. Did you just upgrade to a new
version of gcc thats more standards-compliant, or did someone tweak
the flags?
 
J

John Devereux

Eric Sosman said:
John Devereux wrote:
struct io_descriptor io[64] =
{
100, "Start",
101 ,"Reject",
...


Is this definitely illegal C code that I should fix, or is it just a
warning I can turn off?

It is legal C, but the "fully braced" style

struct io_descriptor io[64] = {
{ 100, "Start" },
{ 101, "Reject" },
...,
};

is often preferable, since the compiler has more information
about the intended layout and may be able to issue better
diagnostics if there's something wrong.

In this particular case there's little chance of making
a mistake that wouldn't be caught immediately: Almost all
valid initializers for `int' are invalid for `char*' and
vice versa, so if you get out of step the compiler is very
likely to detect it. Full braces give little advantage
here, and you may decide they're not worth the bother --
although with a decent editor the bother is pretty small.

It is really just convenience. I have my editor set up to do an
automatic indentation style such that the above would probably tend to
become :-

struct io_descriptor io[64] = {
{
100, "Start"
},
{
101, "Reject"
}
...

The array is quite long and I am more likely to forget a brace than
forget one of the structure components. So I think I will turn off the
warning in this case!
If you choose to use only the outermost braces, the details
of how to suppress a warning (if it's possible, and if
there's any warning to begin with) should be described in
the documentation. Most gcc installations have an "info"
program; give it a whirl.

Sure, I was more concerned about it being an illegal
construct.

Thanks (again),
 
J

John Devereux

Mark McIntyre said:
(snip example of missing braces)


AFAIK you've always needed the braces. Did you just upgrade to a new
version of gcc thats more standards-compliant, or did someone tweak
the flags?

Sorry, you're right! During the holiday I have been playing with an
upgraded gcc (from 3.3.2 to 4.0 & 4.1). I let it loose on my source
code tree and it generated quite a few new warnings related to other
items. But the ones related to braces were in fact there before! I
think it was an IAR compiler (that I used to use) that accepted it.

So I had better fix the code. (Eric Sosman seems to think it isn't
illegal, though).

Thanks,
 

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

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top