Reputation: 2720
I a C source code, I would like to ensure that all elements of my structure are well initilized. It would be easy with the warning Wmissing-field-initializers. But I also would like to be able to initialize my elements using designated initilizer.
for example:
struct s {
int a;
int b;
};
struct s s1 = { .a = 1, .b = 2 };
struct s s2 = { .a = 1 };
struct s s3 = { 1, 2 };
struct s s4 = { 1 };
Let's try to compile this:
$ gcc -Wmissing-field-initializers -c struct_init.c
struct_init.c:9:8: warning: missing initializer
struct_init.c:9:8: warning: (near initialization for ‘s4.b’)
I would like the compilation fails for s2 initilization too. I do prefer designated initiazation, as it facilitates source code reading.
Upvotes: 5
Views: 1961
Reputation: 1167
You could use -Werror to turn warnings into errors or alternatively -Werror=missing-field-initializers to only turn the field initializer warning into an error.
More info here: GCC Warning Options
Edit: I just ran a test using splint with no additional options which gave the following output:
main.c:13:17: Initializer block for s2 has 1 field, but struct s has 2 fields:
<error>
Initializer does not set every field in the structure. (Use -fullinitblock to
inhibit warning)
main.c:15:17: Initializer block for s4 has 1 field, but struct s has 2 fields:
1
It may not be exactly what you are after since you need to run this manually against your source but it will warn about missing designated initializers.
Upvotes: 2
Reputation: 145899
When you use designation initializers, the missing members are initialized to 0
. This is the case even if the object has automatic storage duration. To my knowledge there is no gcc
option that can warn about the members that are not explicitly initialized when you use designation initializers.
Upvotes: 4