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.