Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Brace-enclosed array initialisers

Reply
Thread Tools

Brace-enclosed array initialisers

 
 
Paul Jackson
Guest
Posts: n/a
 
      05-02-2008
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
 
Reply With Quote
 
 
 
 
Flash Gordon
Guest
Posts: n/a
 
      05-02-2008
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
const and array of array (of array ...) Mara Guida C Programming 3 09-03-2009 07:54 AM
Brace-enclosed array initialisers Paul Jackson C Programming 1 05-02-2008 06:00 PM
Bug in gcc4 initialisers suspected Marcel van Kervinck C Programming 34 06-15-2005 08:02 PM
Length of Array of Array of Array Tom Perl Misc 3 12-20-2004 05:23 PM
Abstract constructor initialisers major_tom3 C++ 1 10-14-2003 03:46 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57