473,383 Members | 1,963 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,383 software developers and data experts.

initializing arrays of arrays

Is this legal?

int foo[10][10] = { 0 };

gcc gives:

foo.c: In function `main':
foo.c:5: warning: missing braces around initializer
foo.c:5: warning: (near initialization for `foo[0]')
foo.c:5: warning: unused variable `foo'

And what part of the standard is it explained?
Nov 13 '05 #1
4 24408
Mantorok Redgormor <ne*****@tokyo.com> wrote:
Is this legal?

int foo[10][10] = { 0 };
Yes.
gcc gives:

foo.c: In function `main':
foo.c:5: warning: missing braces around initializer
foo.c:5: warning: (near initialization for `foo[0]')
gcc is trying to be helpful, but in this case it failed.
And what part of the standard is it explained?


Section 6.7.8 in C99

- Kevin.

Nov 13 '05 #2
Mantorok Redgormor wrote:
Is this legal?

int foo[10][10] = { 0 };
It is legal.
gcc gives:

foo.c: In function `main':
foo.c:5: warning: missing braces around initializer
Gcc tells you that it wants { {0} }.
It is free to do so, but { 0 } is correct nevertheless.
And what part of the standard is it explained?


ISO/IEC 9899:1999
6.7.8 Initialization, especially clause 21.

Jirka

Nov 13 '05 #3


Mantorok Redgormor wrote:
Is this legal?

int foo[10][10] = { 0 };

gcc gives:

foo.c: In function `main':
foo.c:5: warning: missing braces around initializer
foo.c:5: warning: (near initialization for `foo[0]')
foo.c:5: warning: unused variable `foo'

And what part of the standard is it explained?

You compiler is set for a high level of warning like -Wall.
So you are getting warnings of things that are ok but might possibly
be wrong. In this cause the warning is not fruitful as the code is
ok.

In the standard:

The Standard says:

6.7.8.21
If there are fewer initializers in a brace-enclosed list than there
are elements or members of an aggregate, or fewer characters in a
string literal used to initialize an array of knownsize than there
are elements in the array, the remainder of the aggregate shall
be initialized implicitly the same as objects that have static storage
duration.

And for static storage:

6.7.8.10
If an object that has static storage duration is not initialized explicitly,
then:
— if it has pointer type, it is initialized to a null pointer;
— if it has arithmetic type, it is initialized to (positive or unsigned)
zero;
— if it is an aggregate, every member is initialized (recursively)
according to these rules;
— if it is a union, the first named member is initialized (recursively)
according to these rules.

So, int foo[10][10] = {0}; is ok.

Your warning probably came from

6.7.8.20
If the aggregate or union contains elements or members that are
aggregates or unions, these rules apply recursively to the
subaggregates or contained unions. If the initializer of
a subaggregate or contained union begins with a left brace, the
initializers enclosed by that brace and its matching right brace
initialize the elements or members of the subaggregate or the
contained union. Otherwise, only enough initializers from the list
are taken to account for the elements or members of the subaggregate
or the first member of the contained union; any remaining
initializers are left to initialize the next element or member of the
aggregate of which the current subaggregate or contained union is a
part.

So, you might silence the warning with:
int foo[10][10] = { {0} };

#include <stdio.h>

int main(void)
{
int i,j, array[10][10] = {{0}};

for(i = 0;i < 10;i++)
{
for(j = 0; j < 10;j++)
printf(" %d",array[i][j]);
putchar('\n');
}
return 0;
}

--
Al Bowers
Tampa, Fl USA
mailto: xa*@abowers.combase.com (remove the x)
http://www.geocities.com/abowers822/

Nov 13 '05 #4

"Mantorok Redgormor" <ne*****@tokyo.com> wrote in message
news:41**************************@posting.google.c om...
Is this legal?

int foo[10][10] = { 0 };

legal.

When you only initialize first few elements in the array, the rest of them
will be initialized to zero.
gcc gives:

foo.c: In function `main':
foo.c:5: warning: missing braces around initializer
foo.c:5: warning: (near initialization for `foo[0]')
foo.c:5: warning: unused variable `foo'

And what part of the standard is it explained?


6.7.8
--
Jeff

je6543 at yahoo dot com

Nov 13 '05 #5

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

Similar topics

1
by: Doug C via .NET 247 | last post by:
Using C#... I am pulling shared memory in to my app that is in the form of apredefined structure. I have arrays in 2 sub-structures. Onearray is an array of another predefined structure, and the...
3
by: Klaus Rudolph | last post by:
Hi all, I have 2 classes, the second one should include an array of the first on. The first class should not have a default constructor. How could I use an array of the first classes? If it is...
8
by: Josh Lessard | last post by:
Given a union definition: union problem_t { int mask; struct { int indices; int ops; } comp; };
5
by: pmatos | last post by:
Hi all, I have a vector of vector of ints, I could use C approach by using int but I think C++ vector<vector<int> > would be easier to manage. So I have a function which creates and initializes...
12
by: jimmij | last post by:
Hi, Please look at the code bellow /*******************/ class ctab { private: static const unsigned n=48;
4
by: jayharris | last post by:
I'm having a ton of trouble initializing a multi-dimensional array inside a constructor, largely because I don't know the size of the array until runtime. I have a class that looks like this: ...
11
by: sg71.cherub | last post by:
Hi All, I have encapsulate CvMat of OpenCV into my own matrix class as the following: class CVMatrix { //== Fields private: unsigned m_Width;
13
by: John | last post by:
Is this a valid C++ program that will not crash on any machine? #include <iostream> using namespace std; int main( void ) { int i; cin >i; double X; X = 1123;
12
by: Mik0b0 | last post by:
Hallo. Let's say, there is a structure struct struct10{ int a1; int a2; int a3; int a4; }count={ {10,20,30,40}, {50,60,70,80}
4
by: Peskov Dmitry | last post by:
class simple_class { int data; public: simple_class() {data=10;}; simple_class(int val) : data(val){} }; int main() {
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: 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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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.