473,657 Members | 2,389 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 24426
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.com base.com (remove the x)
http://www.geocities.com/abowers822/

Nov 13 '05 #4

"Mantorok Redgormor" <ne*****@tokyo. com> wrote in message
news:41******** *************** ***@posting.goo gle.com...
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
8152
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 otheris simply an array of ushort's. Such as: public struct predefinedStruct { public ushort a; public ushort b; } public struct struct1 {
3
1680
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 impossible to initialize the array before entering the constructor , it would never be possible to use arrays of classes in C++ whitout using a default constructor. Is that correct?
8
6712
by: Josh Lessard | last post by:
Given a union definition: union problem_t { int mask; struct { int indices; int ops; } comp; };
5
10566
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 the vector with the values I need (I know these values before hand). - What's the best way to initialize the vector<vector<int> >? Can I initilize it by enumerating its values? - If I do: v = new vector<vector<int> >(3) for example, is it
12
2974
by: jimmij | last post by:
Hi, Please look at the code bellow /*******************/ class ctab { private: static const unsigned n=48;
4
4395
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: class MyClass { public: const int size; MyClass( const int ); };
11
2286
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
2268
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
2281
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
7372
by: Peskov Dmitry | last post by:
class simple_class { int data; public: simple_class() {data=10;}; simple_class(int val) : data(val){} }; int main() {
0
8413
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8324
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8842
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8513
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8617
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7352
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4173
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2742
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1733
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.