multiple named initialization of same member

Discussion in 'C Programming' started by William Ahern, May 9, 2005.

  1. Is the following legal, and if so is the behavior specified?

    struct s {
    int a;
    const char *b;
    };

    int main(void) {
    struct s test = {
    .a = 12,
    .b = "",
    .a = 16,
    };

    return 0;
    }
     
    William Ahern, May 9, 2005
    #1
    1. Advertisements

  2. William Ahern

    Eric Sosman Guest

    William Ahern wrote:
    > Is the following legal, and if so is the behavior specified?
    >
    > struct s {
    > int a;
    > const char *b;
    > };
    >
    > int main(void) {
    > struct s test = {
    > .a = 12,
    > .b = "",
    > .a = 16,
    > };
    >
    > return 0;
    > }


    Yes (assuming C99) and yes. 6.7.8/19:

    "The initialization shall occur in initializer list
    order, each initializer provided for a particular
    subobject overriding any previously listed initializer
    for the same subobject [...]"

    So your initialization produces test.a == 16, test.b pointing
    to an empty string.

    --
     
    Eric Sosman, May 9, 2005
    #2
    1. Advertisements

  3. William Ahern

    Michael Mair Guest

    William Ahern wrote:
    > Is the following legal, and if so is the behavior specified?
    >
    > struct s {
    > int a;
    > const char *b;
    > };
    >
    > int main(void) {
    > struct s test = {
    > .a = 12,
    > .b = "",
    > .a = 16,
    > };
    >
    > return 0;
    > }
    >

    6.7.8 #19 IMO covers this:
    ,-
    | The initialization shall occur in initializer list order, each
    | initializer provided for a particular subobject overriding any
    | previously listed initializer for the same subobject; all
    | subobjects that are not initialized explicitly shall be initialized
    | implicitly the same as objects that have static storage duration.
    `-

    Cheers
    Michael
    --
    E-Mail: Mine is an /at/ gmx /dot/ de address.
     
    Michael Mair, May 9, 2005
    #3
    1. Advertisements

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. =?Utf-8?B?UHVuaXNoZXI=?=

    Need same viewstate for same page across multiple servers

    =?Utf-8?B?UHVuaXNoZXI=?=, Mar 23, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    634
    John Timney \( MVP \)
    Mar 23, 2006
  2. John
    Replies:
    6
    Views:
    748
    Alf P. Steinbach
    Jul 5, 2004
  3. Ricky Lung
    Replies:
    5
    Views:
    3,601
    Richard Herring
    Aug 19, 2004
  4. Replies:
    3
    Views:
    1,061
    Peter_Julian
    Oct 10, 2005
  5. Nagrik
    Replies:
    7
    Views:
    869
    James Kanze
    Feb 9, 2008
  6. Angus
    Replies:
    1
    Views:
    2,993
  7. aaragon
    Replies:
    2
    Views:
    792
    James Kanze
    Nov 2, 2008
  8. Qi
    Replies:
    7
    Views:
    1,063
Loading...