Multi-dimensional array initialization

G

Grumble

Hello,

Is it not legal in C90 to initialize a multi-dimensional array with a
one-dimensional initializer as done below?

int a[2][2][2] = { 1, 2, 3, 4, 5, 6, 7, 8 };

My compiler complains:
example.c:1: warning: missing braces around initializer
example.c:1: warning: (near initialization for `a[0]')

On a somewhat related note, are compilers free to emit warnings, even
if the program is correct?

Nudge
 
T

Tom St Denis

Grumble said:
Hello,

Is it not legal in C90 to initialize a multi-dimensional array with a
one-dimensional initializer as done below?

int a[2][2][2] = { 1, 2, 3, 4, 5, 6, 7, 8 };

My compiler complains:
example.c:1: warning: missing braces around initializer
example.c:1: warning: (near initialization for `a[0]')

I'd just write that as

int a[2][2][2] =
{
{
{1, 2},
{3, 4},
},
{
{5, 6},
{7, 8}
}
};

As such a[0][0] points to {1, 2} and a[1][1] points to {7, 8}.
On a somewhat related note, are compilers free to emit warnings, even
if the program is correct?

As I understand it the compiler is free to emit any warnings it wants. Just
useless warnings will deter users which is a bad thing.

Tom
 
M

Mark McIntyre

Hello,

Is it not legal in C90 to initialize a multi-dimensional array with a
one-dimensional initializer as done below?

int a[2][2][2] = { 1, 2, 3, 4, 5, 6, 7, 8 };

When initialising multidimensional arrays, you need braces round each
level of initialisers.
int a[2][2][2] = { {{1, 2}, {3, 4}},{{ 5, 6}, {7, 8}} };
 
J

Jack Klein

Hello,

Is it not legal in C90 to initialize a multi-dimensional array with a
one-dimensional initializer as done below?

int a[2][2][2] = { 1, 2, 3, 4, 5, 6, 7, 8 };

When initialising multidimensional arrays, you need braces round each
level of initialisers.

No, you don't NEED them. They are recommended as good programming
practice, but the OP's code is perfectly legal in all versions of C
from K&R through C99.
int a[2][2][2] = { {{1, 2}, {3, 4}},{{ 5, 6}, {7, 8}} };
 
J

Jack Klein

Hello,

Is it not legal in C90 to initialize a multi-dimensional array with a
one-dimensional initializer as done below?

int a[2][2][2] = { 1, 2, 3, 4, 5, 6, 7, 8 };

It is perfectly legal in all versions of C to do this.
My compiler complains:
example.c:1: warning: missing braces around initializer
example.c:1: warning: (near initialization for `a[0]')

Did it produce a correct executable? If it did, it is conforming.
On a somewhat related note, are compilers free to emit warnings, even
if the program is correct?

Nudge

So long as they emit at least one diagnostic if a translation unit
contains one or more constraint violations or syntax errors, they are
allowed to do pretty much anything. As far as the standard is
concerned, there is no such thing as a "warning" or "error" message,
that is a particular compiler's distinction.

A compiler which output this to the stdout stream for every
compilation:

"Translation unit might contain an error"

....would be perfectly conforming to the letter of the standard as far
as I can tell, whether the translation unit had any errors or not.

If it always added a second message:

"And you're ugly"

....it would only be a QOI (Quality Of Implementation) issue, not a
standard one.
 

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

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top