Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Missing initializer

Reply
Thread Tools

Missing initializer

 
 
Old Wolf
Guest
Posts: n/a
 
      01-05-2005
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).

 
Reply With Quote
 
 
 
 
Mike Wahler
Guest
Posts: n/a
 
      01-05-2005
"Old Wolf" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> 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


 
Reply With Quote
 
 
 
 
Derrick Coetzee
Guest
Posts: n/a
 
      01-05-2005
Old Wolf wrote:
> 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.
--
Derrick Coetzee
I grant this newsgroup posting into the public domain. I disclaim all
express or implied warranty and all liability. I am not a professional.
 
Reply With Quote
 
Micah Cowan
Guest
Posts: n/a
 
      01-05-2005
Old Wolf wrote:

> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Missing braces around {0} initializer?! Seebs C Programming 27 02-11-2010 10:35 AM
Re: Missing braces around {0} initializer?! Ben Pfaff C Programming 0 02-08-2010 06:19 PM
missing braces around initializer Boon C Programming 3 05-13-2009 07:16 PM
warning: missing braces around initializer Pawel C++ 13 11-14-2008 12:08 PM
missing initializer Roman Mashak C Programming 4 01-04-2007 05:16 AM



Advertisments