struct initialization

Discussion in 'C Programming' started by Leo Havmøller, Aug 19, 2009.

  1. Hi,

    A struct:

    typedef struct
    {
    int a;
    char b;
    long c;
    } testtype;

    A fairly typical way of initializing all field to zero is:

    testtype s1 = { 0 };

    But i recently came across the following:

    testtype s2 = { };

    I.e. nothing between the braces.
    What does the standards say about that?
    Are all fields zeroed like in the first example?

    Leo Havmøller.
     
    Leo Havmøller, Aug 19, 2009
    #1
    1. Advertising

  2. Leo Havmøller <> writes:

    > A struct:
    >
    > typedef struct
    > {
    > int a;
    > char b;
    > long c;
    > } testtype;
    >
    > A fairly typical way of initializing all field to zero is:
    >
    > testtype s1 = { 0 };
    >
    > But i recently came across the following:
    >
    > testtype s2 = { };
    >
    > I.e. nothing between the braces.
    > What does the standards say about that?


    It's a syntax error.

    > Are all fields zeroed like in the first example?


    If it is accepted as an extension, I'd expect that to be the case, but
    it is not ISO C.

    --
    Ben.
     
    Ben Bacarisse, Aug 19, 2009
    #2
    1. Advertising


  3. > testtype s2 = { };
    >
    > I.e. nothing between the braces.
    > What does the standards say about that?


    That it isn't allowed.

    > Are all fields zeroed like in the first example?


    If your compiler supports it, that is most likely the case.



    Igmar
     
    Igmar Palsenberg, Aug 19, 2009
    #3
  4. Thank you very much for the responses, everyone.
    I saw it in some code that AFAIK was written this year.
    It is currently compiled with GCC 4.3.3, but it will also be used with other
    compilers, so i think i will locate it again and fix it.

    Leo Havmøller.
     
    Leo Havmøller, Aug 20, 2009
    #4
    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. JKop
    Replies:
    10
    Views:
    975
  2. Chris Fogelklou
    Replies:
    36
    Views:
    1,433
    Chris Fogelklou
    Apr 20, 2004
  3. Matthias Kaeppler
    Replies:
    2
    Views:
    465
    Victor Bazarov
    Jul 18, 2005
  4. Replies:
    6
    Views:
    476
    Ron Natalie
    Dec 11, 2005
  5. Replies:
    2
    Views:
    758
    David Harmon
    Sep 20, 2006
Loading...

Share This Page