Missing initializer

O

Old Wolf

For the following code:

struct S
{
char t[5][5];
int flags;
};

void func(void)
{
struct S s = { 0 };
}

I get a "warning: missing initializer" from gcc in C99 mode.
Is there something wrong with my code or is this just
paranoia from the compiler. (My intention was that all
members of 's' be zero-initialized).
 
M

Mike Wahler

Old Wolf said:
For the following code:

struct S
{
char t[5][5];
int flags;
};

void func(void)
{
struct S s = { 0 };
}

I get a "warning: missing initializer" from gcc in C99 mode.
Is there something wrong with my code


Not that I can see.
or is this just
paranoia from the compiler. (My intention was that all
members of 's' be zero-initialized).

The above should do just that. Perhaps the following
form will remove the warning:

struct S s = {{0}};

Or be very explicit:

struct S s = {{0}, 0};

BTW what does the gcc documentation say about this warning?

-Mike
 
D

Derrick Coetzee

Old said:
struct S
{
char t[5][5];
int flags;
};

void func(void)
{
struct S s = { 0 };
}

I get a "warning: missing initializer" from gcc in C99 mode.

Marked OT since my post is mostly about gcc. I get:

temp.c: In function `func':
temp.c:9: warning: missing braces around initializer
temp.c:9: warning: (near initialization for `s.t')
temp.c:9: warning: unused variable `s'

These warnings all make sense (implicit braces are a nasty source of
potential errors and good to warn about). I don't see yours. My
command-line is:

gcc -std=c99 -Wall temp.c -o temp

Perhaps you misread it? If not, perhaps you have an older version of gcc
with a known bug? I have 3.3.4 20040623.
 
M

Micah Cowan

Old said:
For the following code:

struct S
{
char t[5][5];
int flags;
};

void func(void)
{
struct S s = { 0 };
}

I get a "warning: missing initializer" from gcc in C99 mode.
Is there something wrong with my code or is this just
paranoia from the compiler. (My intention was that all
members of 's' be zero-initialized).

struct S s = {{{0}},0};

seems to do the trick. The outermost braces are for the struct,
the next set for the array of arrays, and the innermost set for
the array of char. This also shuts up the missing brace warning
that you would get with

gcc --std=c00 -W -Wall -c test.c

HTH, HAND.
 

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,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top