Brace-enclosed array initialisers

Discussion in 'C Programming' started by Paul Jackson, May 2, 2008.

  1. Paul Jackson

    Paul Jackson Guest

    Here's a brace-enclosed initialiser for a 3D array:

    int a4[2][3][4] = {
    { 1, 2, 3, 4, {5, 6, 7 }, },
    { {13, 14, 15 }, 17, 18, 19, 20, {21, 22, 23, 24} }};

    gcc produces some warnings when compiling this, but sets a4 to this,
    which is what I'd expect:

    { { 1 2 3 4 } { 5 6 7 0 } {0 0 0 0 } }
    { {13 14 15 0 } {17 18 19 20 } {21 22 23 24 } }

    It gets more interesting if I leave out initialiser '20':

    int a4[2][3][4] = {
    { 1, 2, 3, 4, {5, 6, 7 }, },
    { {13, 14, 15 }, 17, 18, 19, {21, 22, 23, 24} }};

    gcc now produces lots more warnings, and sets a4 to this:

    { { 1 2 3 4 } { 5 6 7 0 } { 0 0 0 0 } }
    { {13 14 15 0 } {17 18 19 21 } { 0 0 0 0 } }

    Any ideas on what the 'correct' behaviour here should be? I've only
    checked 6.7.8 in the C99 spec, but this seems to be wrong. I suspect
    that either reporting an error or setting a4 to

    { { 1 2 3 4 } { 5 6 7 0 } { 0 0 0 0 } }
    { {13 14 15 0 } {17 18 19 0 } {21 22 23 24 } }

    would have better.

    Thanks -

    Paul
    Paul Jackson, May 2, 2008
    #1
    1. Advertising

  2. Paul Jackson

    Flash Gordon Guest

    Victor Bazarov wrote, On 02/05/08 13:21:
    > Paul Jackson wrote:
    >> Here's a brace-enclosed initialiser for a 3D array:
    >>
    >> int a4[2][3][4] = {
    >> { 1, 2, 3, 4, {5, 6, 7 }, },
    >> { {13, 14, 15 }, 17, 18, 19, 20, {21, 22, 23, 24} }};
    >>
    >> gcc produces some warnings when compiling this, but sets a4 to this,
    >> which is what I'd expect:


    It is, of course, allowed to produce warnings.

    >> { { 1 2 3 4 } { 5 6 7 0 } {0 0 0 0 } }
    >> { {13 14 15 0 } {17 18 19 20 } {21 22 23 24 } }
    >>
    >> It gets more interesting if I leave out initialiser '20':
    >>
    >> int a4[2][3][4] = {
    >> { 1, 2, 3, 4, {5, 6, 7 }, },


    I think this part is correct. I believe that footnote 129 makes it
    somewhat clearer.

    >> { {13, 14, 15 }, 17, 18, 19, {21, 22, 23, 24} }};


    I think that this part should produce a diagnostic.

    >> gcc now produces lots more warnings, and sets a4 to this:
    >>
    >> { { 1 2 3 4 } { 5 6 7 0 } { 0 0 0 0 } }


    The above would have been correct.

    >> { {13 14 15 0 } {17 18 19 21 } { 0 0 0 0 } }
    >> Any ideas on what the 'correct' behaviour here should be? I've only
    >> checked 6.7.8 in the C99 spec, but this seems to be wrong. I suspect
    >> that either reporting an error or setting a4 to
    >>
    >> { { 1 2 3 4 } { 5 6 7 0 } { 0 0 0 0 } }
    >> { {13 14 15 0 } {17 18 19 0 } {21 22 23 24 } }
    >>
    >> would have better.

    >
    > The point of the definition (not sure about C99, too lazy to check,
    > using C++03, as you ought to) is that in your example with '20' left
    > out, the "17, 18, 19" fill the row a4[1][1], and since they are not
    > enclosed in their own braces, the following curly brace (before the
    > '21') is supposed to enclose the initialiser list for a4[1][1][3].
    > AFAICT, since there are more initialisers in the braced list starting
    > with '21', the entire initialiser is ill-formed. GCC lets it slide,
    > apparently, and simply ignores '22, 23, 24' initialising a4[1][1][3]
    > with only '21'. The rest of the array it leaves uninitialised.


    In C the rest of the array is definitely *not* left uninitialised. If
    you initialise part of an aggregate type (e.g. an array) then the C
    standard specifies that all members are initialised. The only exception
    to this is unnamed members of a structure objects.

    I suspect the rules are similar for C++.

    > And
    > did you try to use 'strict' mode?


    Yes, always worth using "-ansi -pedantic" or "-std=c99 -pedantic" if you
    want as close as it comes to C99 conformance. See
    http://gcc.gnu.org/c99status.html for information on C99 conformance.
    --
    Flash Gordon
    Flash Gordon, May 2, 2008
    #2
    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. koslows
    Replies:
    0
    Views:
    523
    koslows
    May 12, 2005
  2. major_tom3

    Abstract constructor initialisers

    major_tom3, Oct 13, 2003, in forum: C++
    Replies:
    1
    Views:
    1,056
    Ryan Winter
    Oct 14, 2003
  3. Marcel van Kervinck

    Bug in gcc4 initialisers suspected

    Marcel van Kervinck, May 17, 2005, in forum: C Programming
    Replies:
    34
    Views:
    867
  4. Paul Jackson

    Brace-enclosed array initialisers

    Paul Jackson, May 2, 2008, in forum: C++
    Replies:
    1
    Views:
    367
    Flash Gordon
    May 2, 2008
  5. BartC

    'Too many initialisers'

    BartC, Feb 15, 2014, in forum: C Programming
    Replies:
    5
    Views:
    92
    Ben Bacarisse
    Feb 16, 2014
Loading...

Share This Page