Initialize struct fileds to zero

V

vashwath

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?
 
U

usr.root

(e-mail address removed) 写é“:
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 !
 
S

S.Tobias

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'.
 
V

vashwath

Thanks for the reply Tobias.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?
 
F

Flash Gordon

(e-mail address removed) 写é“:

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.

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);
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 lineSince it is only the value of the pointer being printed, not what it
points to, this is fine.
 
V

vashwath

Hi all,
Is there any other method of initializing structure for which GCC will
not give any warnings when compiled with -W option?
 
S

S.Tobias

Thanks for the reply Tobias.
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;
 
S

Stan Milam

Thanks for the reply Tobias.


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.
 
K

Keith Thompson

Stan Milam said:
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.
 
V

vashwath

I have to compile using -W option because this is one of the
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?
 
K

Keith Thompson

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.
 
K

Keith Thompson

Thanks for the reply Tobias.
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".
 

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

Staff online

Members online

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top