469,887 Members | 1,275 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,887 developers. It's quick & easy.

Initializers for arrays of structures

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,

--

John Devereux
Nov 14 '05 #1
4 1550


John Devereux wrote:
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.

--
Er*********@sun.com

Nov 14 '05 #2
On 28 Mar 2005 21:30:23 +0100, in comp.lang.c , John Devereux
<jd******@THISdevereux.me.uk> wrote:
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?

--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>
Nov 14 '05 #3
Eric Sosman <er*********@sun.com> writes:
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),

--

John Devereux
Nov 14 '05 #4
Mark McIntyre <ma**********@spamcop.net> writes:
On 28 Mar 2005 21:30:23 +0100, in comp.lang.c , John Devereux
<jd******@THISdevereux.me.uk> wrote:
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?


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,

--

John Devereux
Nov 14 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Java script Dude | last post: by
6 posts views Thread by Neil Zanella | last post: by
33 posts views Thread by Peter Seaman | last post: by
reply views Thread by Art Cummings | last post: by
1 post views Thread by r035198x | last post: by
4 posts views Thread by maria | last post: by
127 posts views Thread by sanjay.vasudevan | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.