469,610 Members | 2,305 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,610 developers. It's quick & easy.

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 18628

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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Kai Wu | last post: by
7 posts views Thread by Andi.Martin | last post: by
10 posts views Thread by FBM | last post: by
12 posts views Thread by NewToCPP | last post: by
8 posts views Thread by aaragon | last post: by
4 posts views Thread by June Lee | last post: by
9 posts views Thread by void main | last post: by
reply views Thread by devrayhaan | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.