Initializers for arrays of structures

Discussion in 'C Programming' started by John Devereux, Mar 28, 2005.

  1. 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
     
    John Devereux, Mar 28, 2005
    #1
    1. Advertising

  2. John Devereux

    Eric Sosman Guest

    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.

    --
     
    Eric Sosman, Mar 28, 2005
    #2
    1. Advertising

  3. On 28 Mar 2005 21:30:23 +0100, in comp.lang.c , John Devereux
    <> 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>
     
    Mark McIntyre, Mar 28, 2005
    #3
  4. Eric Sosman <> 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
     
    John Devereux, Mar 28, 2005
    #4
  5. Mark McIntyre <> writes:

    > On 28 Mar 2005 21:30:23 +0100, in comp.lang.c , John Devereux
    > <> 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
     
    John Devereux, Mar 28, 2005
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Glen Able

    Class variable initializers

    Glen Able, Jan 29, 2004, in forum: Java
    Replies:
    0
    Views:
    345
    Glen Able
    Jan 29, 2004
  2. j l
    Replies:
    5
    Views:
    407
    A. Bolmarcich
    Feb 23, 2004
  3. tweak
    Replies:
    14
    Views:
    2,817
    Eric Sosman
    Jun 11, 2004
  4. Alfonso Morra
    Replies:
    11
    Views:
    752
    Emmanuel Delahaye
    Sep 24, 2005
  5. Philipp
    Replies:
    21
    Views:
    1,183
    Philipp
    Jan 20, 2009
Loading...

Share This Page