473,385 Members | 1,356 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Brace-enclosed array initialisers

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
Jun 27 '08 #1
2 2104
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:

{ { 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.
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. And
did you try to use 'strict' mode?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 27 '08 #2
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
Jun 27 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Andy Fish | last post by:
Hi, I am trying to parse a file using Xerces' DOMParser.parse(String systemId) I have come across a problem where it refuses to open a file with curly braces "{" or "}" in the file name. I...
102
by: Skybuck Flying | last post by:
Sometime ago on the comp.lang.c, I saw a teacher's post asking why C compilers produce so many error messages as soon as a closing bracket is missing. The response was simply because the compiler...
4
by: carl.manaster | last post by:
Hi, Group, I prefer indenting braces with their block, thus: public int Foo() .....{ .....Bar(); .....}
4
by: Jason Kendall | last post by:
How do I use a curly brace within a string passed to String.Format? I want to pass a string that includes a curly brace, but that curly brace is not being used to indicate a replacable format...
5
by: krbyxtrm | last post by:
Hi is there a way to 'manage' function execution using macros? #define MY_CALL_MACRO(MacroName) { g_MacroStack.push_back(MacroName); <some code here...>} such that when i use the...
5
by: singhm | last post by:
Anybody know how to fix this?????????? ERROR: 1>c:\cs 140\assignment 5\assignment 5\main.cpp(88) : fatal error C1075: end of file found before the left brace '{' at 'c:\cs 140\assignment...
2
by: GC2006 | last post by:
//Error 1 error C2601: 'computeBestHotelBestMonth' : local function definitions are illegal e:\project 3\prj 3b.cpp 90 //Error 2 fatal error C1075: end of file found before the left brace '{' at...
8
by: case.learning | last post by:
Hi everyone, I'm doing a C problem checking for rudimentary syntax errors like unbalanced parentheses, brackets, and braces. I only know parentheses () or braces {}, but do not know what...
2
by: Bob Altman | last post by:
Hi all, In the VS 2005 IDE, is there an easy way to navigate to the closing brace for a given openeing brace in the source code? TIA - Bob
6
by: Bryan Parkoff | last post by:
I want to know the best practice to write on both C / C++ source codes. The best practice is to ensure readable. Should left brace be after function or if on the same line or next line. For...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.