initializing arrays of arrays

M

Mantorok Redgormor

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?
 
K

Kevin Easton

Mantorok Redgormor said:
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.
 
J

Jirka Klaue

Mantorok said:
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
 
A

Al Bowers

Mantorok said:
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[j]);
putchar('\n');
}
return 0;
}
 
J

Jeff

Mantorok Redgormor said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top