473,382 Members | 1,464 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 struct fileds to zero

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 initializer
initStructElem.c:20: warning: (near initialization for `str1.f')

Here is the program

1 #include <stdio.h>
2
3 int main(void)
4 {
5 typedef struct
6 {
7 int i1;
8 float f1;
9 }str_1t;
10
11 typedef struct
12 {
13 int i;
14 float f;
15 char c;
16 int *pi;
17 str_1t s2;
18 }str_t;
19
20 str_t str1={0};
21
22 printf("str1.i=%d\t str1.f=%f\t str1.c=%d\t
str1.pi=%p\n",str1.i,str1 .f,str1.c,str1.pi);
23 printf("str1.s2.i1=%d\n",str1.s2.i1);
24 return 0;
25 }
Please let me know why gcc is complaining.
Is this not a standard of way of intializing structure fields?

Nov 15 '05 #1
11 19385

va******@rediffmail.com 写道:
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 initializer
initStructElem.c:20: warning: (near initialization for `str1.f')

Here is the program

1 #include <stdio.h>
2
3 int main(void)
4 {
5 typedef struct
6 {
7 int i1;
8 float f1;
9 }str_1t;
10
11 typedef struct
12 {
13 int i;
14 float f;
15 char c;
16 int *pi;
17 str_1t s2;
18 }str_t;
19
20 str_t str1={0};
21
22 printf("str1.i=%d\t str1.f=%f\t str1.c=%d\t
str1.pi=%p\n",str1.i,str1 .f,str1.c,str1.pi);
23 printf("str1.s2.i1=%d\n",str1.s2.i1);
24 return 0;
25 }
Please let me know why gcc is complaining.
Is this not a standard of way of intializing structure fields?


the problem is the "int *pi;",it's a pointer ,you must intializing it
before use it !

Nov 15 '05 #2
va******@rediffmail.com wrote:
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 initializer
initStructElem.c:20: warning: (near initialization for `str1.f')

Here is the program
[snip] 5 typedef struct
6 {
7 int i1;
8 float f1;
9 }str_1t;
10
11 typedef struct
12 {
13 int i;
14 float f;
15 char c;
16 int *pi;
17 str_1t s2;
18 }str_t;
19
20 str_t str1={0}; [snip]
Please let me know why gcc is complaining.
Because you've invoked it in supercalifragilipedantic mode.
It wants you to give explicit initializers for all struct
members:
str_t str1={0,0,0,0,0,0};
(ie.: str1.i, str1.f, str1.c, str1.pi, str1.s2.i1, str1.s2.f1).
Have a look in the compiler docs what `-W' option does.
Is this not a standard of way of intializing structure fields?


str_t str1={0};
is perfectly stardard and defined.

[OT] Perhaps you want `-Wall -pedantic' options instead of `-W'.

--
Stan Tobias
mailx `echo si***@FamOuS.BedBuG.pAlS.INVALID | sed s/[[:upper:]]//g`
Nov 15 '05 #3
Thanks for the reply Tobias.
Perhaps you want `-Wall -pedantic' options instead of `-W'.

I have to compile using -W option because this is one of the
requirement from my client. Is there other way of intializing all the
fields of big structures to 0?

Nov 15 '05 #4
us******@gmail.com wrote:
va******@rediffmail.com 写道:

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 initializer
initStructElem.c:20: warning: (near initialization for `str1.f')

Here is the program

1 #include <stdio.h>
2
3 int main(void)
4 {
5 typedef struct
6 {
7 int i1;
8 float f1;
9 }str_1t;
10
11 typedef struct
12 {
13 int i;
14 float f;
15 char c;
16 int *pi;
17 str_1t s2;
18 }str_t;
19
20 str_t str1={0};
The above line is correct as far as the standard is concerned.
Personally I would be inclined to just disable that warning in gcc, but
the specifics of how to do that are not on topic here.
21
22 printf("str1.i=%d\t str1.f=%f\t str1.c=%d\t
str1.pi=%p\n",str1.i,str1 .f,str1.c,str1.pi);
This is one instance when a cast is required since %p requires a pointer
to void but str1.pi is a pointer to int.
printf("str1.i=%d\t str1.f=%f\t str1.c=%d\t str1.pi=%p\n",
str1.i,str1.f,str1.c,(void*)str1.pi);
23 printf("str1.s2.i1=%d\n",str1.s2.i1);
24 return 0;
25 }
Please let me know why gcc is complaining.
Is this not a standard of way of intializing structure fields?


the problem is the "int *pi;",it's a pointer ,you must intializing it
before use it !


It *has* been initialised to a null pointer by the line
20 str_t str1={0};

Since it is only the value of the pointer being printed, not what it
points to, this is fine.
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
Nov 15 '05 #5
Hi all,
Is there any other method of initializing structure for which GCC will
not give any warnings when compiled with -W option?

Nov 15 '05 #6
va******@rediffmail.com wrote:
Thanks for the reply Tobias.
Perhaps you want `-Wall -pedantic' options instead of `-W'.
I have to compile using -W option because this is one of the
requirement from my client.


Why don't you ask your client how he wants his structs initialized then?
Is there other way of intializing all the
fields of big structures to 0?


A macro per each struct might help you:

struct bar { /*...*/ };
#define bar_zero_initializer {0, 0 /*...*/}

struct foo_with_bar { /*...*/ struct bar b; /*...*/ };
#define foo_with_bar_zero_initializer \
{0, /*...*/ bar_zero_initializer, 0, /*...*/}

const struct foo_with_bar cfwb = foo_with_bar_zero_initializer;

--
Stan Tobias
mailx `echo si***@FamOuS.BedBuG.pAlS.INVALID | sed s/[[:upper:]]//g`
Nov 15 '05 #7
va******@rediffmail.com wrote:
Thanks for the reply Tobias.
Perhaps you want `-Wall -pedantic' options instead of `-W'.


I have to compile using -W option because this is one of the
requirement from my client. Is there other way of intializing all the
fields of big structures to 0?

man memset.
Nov 15 '05 #8
Stan Milam <st*****@swbell.net> writes:
va******@rediffmail.com wrote:
Thanks for the reply Tobias.
Perhaps you want `-Wall -pedantic' options instead of `-W'.

I have to compile using -W option because this is one of the
requirement from my client. Is there other way of intializing all the
fields of big structures to 0?

man memset.


memset() will not portably set pointers or floating-point objects to
zero (NULL or 0.0, respectively). It will happen to do so on many
systems, but you shouldn't depend on it.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 15 '05 #9
>>>I have to compile using -W option because this is one of the
requirement from my client. Is there other way of intializing all the
fields of big structures to 0?

man memset.


memset() will not portably set pointers or floating-point objects to
zero (NULL or 0.0, respectively). It will happen to do so on many
systems, but you shouldn't depend on it.


Are there any other standard,Portable methods of doing this?

Nov 15 '05 #10
va******@rediffmail.com writes:
I have to compile using -W option because this is one of the
requirement from my client. Is there other way of intializing all the
fields of big structures to 0? man memset.

memset() will not portably set pointers or floating-point objects to
zero (NULL or 0.0, respectively). It will happen to do so on many
systems, but you shouldn't depend on it.


Are there any other standard,Portable methods of doing this?


Yes. You can either provide explicit initial values for all the
members, or you can use { 0 }. The latter is legal in both C90 and
C99; all unspecified members are initialized the same as objects with
static storage duration (i.e., set to 0, or NULL, or 0.0, or whatever
as appropriate for the type).

If you want to know how to use { 0 } without getting a spurious
warning from gcc, I suggest asking in gnu.gcc.help.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 15 '05 #11
va******@rediffmail.com writes:
Thanks for the reply Tobias.
Perhaps you want `-Wall -pedantic' options instead of `-W'.

I have to compile using -W option because this is one of the
requirement from my client. Is there other way of intializing all the
fields of big structures to 0?


I've looked into this a bit more, reading the gcc documentation.
Strictly speaking this is off-topic, but I think it's worth mentioning
because people are often advised here to use gcc's "-W" option.

The following `-W...' options are not implied by `-Wall'. Some of
them warn about constructions that users generally do not consider
questionable, but which occasionally you might wish to check for;
others warn about constructions that are necessary or hard to avoid in
some cases, and there is no simple way to modify the code to suppress
the warning.

`-Wextra'
(This option used to be called `-W'. The older name is still
supported, but the newer name is more descriptive.) Print extra
warning messages for these events:

[...]

* An aggregate has an initializer which does not initialize all
members. This warning can be independently controlled by
`-Wmissing-field-initializers'.

The original problem was something like this:

struct s {
int a;
void *b;
};
struct s obj = { 0 };

The warning appears because there's no explicit initializer for b, but
the code is perfectly valid. In fact, it's arguably better than
providing explicit initializations for all the members, since it
doesn't have to be changed when the struct definition is changed.

If you're required to use gcc's "-W" option, and your code is not
allowed to produce any warnings, then you're effectively programming
in a restricted subset of C.

Personally, I tend to use "-W -Wall", but I feel free to ignore
warnings that don't make sense. If you don't have that freedom,
that's a matter between you and your client.

The simplest solution is probably to provide explicit initializers
for all the members; in the sample above:
struct s obj = { 0, NULL };

IMHO, gcc could be improved by treating { 0 } as a special case, not
affected by "-Wmissing-field-initializers".

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 15 '05 #12

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

Similar topics

6
by: Kai Wu | last post by:
#include <string.h> #include <fstream> #include <time.h> typedef unsigned char BYTE; struct Dex { BYTE status; struct timeval timestamp; }; int main(){
7
by: Andi.Martin | last post by:
Hi, how is it possible, to only initialize parts of a structure. Example: typedef struct{ int a, b; ... (huge lot of members); double x,y;
16
by: Chad | last post by:
How do I set the value of a = 3 in the following lines of code? #include <stdio.h> struct letter{ char a; }; struct add { struct letter addit;
10
by: FBM | last post by:
Hi, I have a doubt regarding structus.. I have to following: typedef struct STATS { int src; int dest; int total; int drop;
12
by: NewToCPP | last post by:
does the default constructor initialize values? I have a class as defined below: class A { int i; char c; int * iPtr;
10
by: wenmang | last post by:
hi, I have following: struct array1 { int id; char *name; }; struct array2 {
8
by: aaragon | last post by:
Hi, just a very simple question. I was wondering what is the most efficient way of initializing an array. I have a very simple class that wraps an array to provide bound checking, something like...
4
by: June Lee | last post by:
what is it means by {0}, is that means initialize a struct to NULL? ne_uri uri = {0}; typedef struct { char *scheme; char *host, *userinfo; unsigned int port; char *path, *query, *fragment;...
9
by: void main | last post by:
I'm rather new to complex numbers in C and was wondering, how do I initialize a complex variable properly if the imaginary part is 0. I tried -------- #include <complex.h> float complex c...
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: 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
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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.