initialize structs

F

FBM

Hi,

I have a doubt regarding structus.. I have to following:

typedef struct STATS
{
int src;
int dest;
int total;
int drop;
} STATS;

And then, the following:

STATS e_STATS[10];

I want my e_STATS[10] initialize to 0... however, i tried several ways
and i cannot make it work..

tnx,
Fernando
 
F

FBM

Got it.. e_STATS is an int *... so, the one way is to do 'memset'...
sorry, i didnt think it through..

tnx,
Fernando
 
M

Michael Brennan

FBM said:
Hi,

I have a doubt regarding structus.. I have to following:

typedef struct STATS
{
int src;
int dest;
int total;
int drop;
} STATS;

And then, the following:

STATS e_STATS[10];

I want my e_STATS[10] initialize to 0... however, i tried several ways
and i cannot make it work..

Change the declaration to

STATS e_STATS[10] = { 0 };

and it will be initialized to 0.

/Michael
 
F

FBM

Thanks.. it's working, but i get this nasty warning..

STATS mySTATS[10] = { 0 };

gcc -DMEMWATCH -O0 -g3 -Wall -c -fmessage-length=0 -omain.o ../main.c
.../main.c: In function 'main':
.../main.c:44: warning: missing braces around initializer
.../main.c:44: warning: (near initialization for 'mySTATS[0]')
Finished building: ../main.c

Any idea why?

tnx,
Fernando
 
P

pete

FBM said:
Thanks.. it's working, but i get this nasty warning..

STATS mySTATS[10] = { 0 };

gcc -DMEMWATCH -O0 -g3 -Wall -c -fmessage-length=0 -omain.o ../main.c
../main.c: In function 'main':
../main.c:44: warning: missing braces around initializer
../main.c:44: warning: (near initialization for 'mySTATS[0]')
Finished building: ../main.c

Any idea why?

Probably has something to do with the code
that's on or near line 44.
 
E

Eric Sosman

FBM wrote On 05/01/06 10:48,:
Hi,

I have a doubt regarding structus.. I have to following:

typedef struct STATS
{
int src;
int dest;
int total;
int drop;
} STATS;

And then, the following:

STATS e_STATS[10];

I want my e_STATS[10] initialize to 0... however, i tried several ways
and i cannot make it work..

If e_STATS has "static storage duration" (that is, if
is defined outside any function or if it is defined with the
`static' keyword), it will be initialized to zero. Since you
say it isn't zeroed, I guess you are defining it inside a
function and it is an "automatic" variable; these are not
initialized unless you initialize them. The easiest way is

STATS e_STATS[10] = { 0 };
 
F

Flash Gordon

FBM said:
Got it.. e_STATS is an int *... so, the one way is to do 'memset'...
sorry, i didnt think it through..

Please provide context, even when replying to yourself. There is no
guarantee that everyone has seen (or ever will see) the message you are
replying to. Even if they have seen it they may not remember it. Google
is not Usenet just a poor excuse for an interface to it. See
http://clc-wiki.net/wiki/Intro_to_clc specifically the bit about Google.

Secondly, arrays are *not* pointers, and in particular an array of
structs is *not* a pointer to int! It is a *very* long way from it. See
the comp.lang.c FAQ http://c-faq.com/ specifically question 6.9, but the
rest of section 6 and the entire FAQ as well.
 
A

Amit Limaye

just one thing only 0 initialization is guranteed
dont try nething else it is not :)
i got bit by this once so just a warning

-SIGTERM
amit
 
B

Ben Pfaff

Amit Limaye said:
just one thing only 0 initialization is guranteed
dont try nething else it is not :)
i got bit by this once so just a warning

It sounds like this may have been a bug in your implementation,
although you didn't give enough details to tell.
 
B

Barry Schwarz

Thanks.. it's working, but i get this nasty warning..

STATS mySTATS[10] = { 0 };

gcc -DMEMWATCH -O0 -g3 -Wall -c -fmessage-length=0 -omain.o ../main.c
../main.c: In function 'main':
../main.c:44: warning: missing braces around initializer
../main.c:44: warning: (near initialization for 'mySTATS[0]')
Finished building: ../main.c
You probably need {{0}}. The outer set of braces denotes
initialization data. The inner set indicates it applies to the first
element of the array (mySTATS[0]). Since there are less initializers
than members of mySTATS[0], the first member is initialized explicitly
and the remaining members are set to the appropriate form of 0
implicitly. Since there are less array initializers than elements of
the array, the remaining array elements are initialized to 0
implicitly.

Many implementations would accept your code and I don't know if the
standard *requires* the second set of braces. It could be
over-exuberance on the part of gcc. Compilers are allowed to generate
"extraneous" diagnostics for anything they don't like as long as they
generate the correct code. (I'm waiting for one that complains the
indenting style is unreadable for some of the code posted here.)


Remove del for email
 
F

Flash Gordon

Barry said:
Thanks.. it's working, but i get this nasty warning..

STATS mySTATS[10] = { 0 };

gcc -DMEMWATCH -O0 -g3 -Wall -c -fmessage-length=0 -omain.o ../main.c
../main.c: In function 'main':
../main.c:44: warning: missing braces around initializer
../main.c:44: warning: (near initialization for 'mySTATS[0]')
Finished building: ../main.c
You probably need {{0}}. The outer set of braces denotes
initialization data. The inner set indicates it applies to the first
element of the array (mySTATS[0]). Since there are less initializers
than members of mySTATS[0], the first member is initialized explicitly
and the remaining members are set to the appropriate form of 0
implicitly. Since there are less array initializers than elements of
the array, the remaining array elements are initialized to 0
implicitly.

Many implementations would accept your code and I don't know if the
standard *requires* the second set of braces.

The standard definitely does *not* require the second set of braces.
> It could be
over-exuberance on the part of gcc. Compilers are allowed to generate
"extraneous" diagnostics for anything they don't like as long as they
generate the correct code. (I'm waiting for one that complains the
indenting style is unreadable for some of the code posted here.)

The gcc warning is often useful IMHO but, in this case, it isn't. It
would be nice if they had an exception for the {0} initialiser.
 

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