473,382 Members | 1,204 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

initialize structs

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

May 1 '06 #1
10 2412
FBM
Got it.. e_STATS is an int *... so, the one way is to do 'memset'...
sorry, i didnt think it through..

tnx,
Fernando

May 1 '06 #2
FBM wrote:
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
May 1 '06 #3
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

May 1 '06 #4
FBM wrote:

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.

--
pete
May 1 '06 #5


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 };

--
Er*********@sun.com

May 1 '06 #6
FBM wrote:
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.
--
Flash Gordon, living in interesting times.
Web site - http://home.flash-gordon.me.uk/
comp.lang.c posting guidelines and intro:
http://clc-wiki.net/wiki/Intro_to_clc
May 1 '06 #7
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

May 1 '06 #8
"Amit Limaye" <am*********@gmail.com> writes:
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.
--
int main(void){char p[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv wxyz.\
\n",*q="kl BIcNBFr.NKEzjwCIxNJC";int i=sizeof p/2;char *strchr();int putchar(\
);while(*q){i+=strchr(p,*q++)-p;if(i>=(int)sizeof p)i-=sizeof p-1;putchar(p[i]\
);}return 0;}
May 1 '06 #9
On 1 May 2006 08:32:47 -0700, "FBM" <fb******@gmail.com> wrote:
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
May 2 '06 #10
Barry Schwarz wrote:
On 1 May 2006 08:32:47 -0700, "FBM" <fb******@gmail.com> wrote:
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.
--
Flash Gordon, living in interesting times.
Web site - http://home.flash-gordon.me.uk/
comp.lang.c posting guidelines and intro:
http://clc-wiki.net/wiki/Intro_to_clc
May 2 '06 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: news.microsoft.com | last post by:
Hi, I am using structs and am also using property accessors to access those private member fields... TO me this is a good way of handling them, but I find alot of people using direct access to...
6
by: James Pascoe | last post by:
Dear All, Apologies if this is OT. I have a C program which processes an arbitrary number of structs that are stored in a hash table. (The nature of the processing and the layout of the...
11
by: vashwath | last post by:
Hi all, The following program is compiled using gcc with "-W" option. GCC is giving the following warning message initStructElem.c: In function `main': initStructElem.c:20: warning: missing...
5
by: Paminu | last post by:
Why make an array of pointers to structs, when it is possible to just make an array of structs? I have this struct: struct test { int a; int b;
5
by: Bilgehan.Balban | last post by:
Hi, I am currently brushing up my c++ knowledge and I would like to ask you about the differences between classes and C structs, in the function/method perspective. 1) Is it correct to say...
61
by: Marty | last post by:
I am new to C# and to structs so this could be easy or just not possible. I have a struct defined called Branch If I use Branch myBranch = new Branch(i); // everything works If I use Branch...
29
by: Dom | last post by:
I'm really confused by the difference between a Struct and a Class? Sometimes, I want just a group of fields to go together. A Class without methods seems wrong, in that it carries too much...
43
by: JohnQ | last post by:
Are a default constructor, destructor, copy constructor and assignment operator generated by the compiler for a struct if they are not explicitely defined? I think the answer is yes, because...
5
by: Bob Altman | last post by:
Hi all, Here's a really basic C++ syntax question: How do I initialize an array, whose size is a literal, to all zeros in its initializer without coding a loop to do it? For example: ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.