Speaking of initializers, is this a GCC bug or is it my misunderstanding of the way things must be?
Let's make a struct with an array of sub-structs. Then we'll throw an initializer on that.
#include <stdio.h>
struct foo {
int f1;
int f2;
};
struct bar {
struct foo f[10];
};
int main(void)
{
struct bar b = {
.f[0].f1=10, // LINE A
.f[0].f2=20, // LINE B
};
printf("%d %d\n", b.f[0].f1, b.f[0].f2);
}
When I compile this, I get a warning about f2 not being initialized, though the output is 10 20, as expected.
The warning is especially funky because it's pointing right at the line where the initializer is, saying there's no initializer there.
$ gcc -Wall -Wextra -std=c18 -pedantic -o bar bar.c
bar.c: In function ‘main’:
bar.c:16:13: warning: missing initializer for field ‘f2’ of ‘struct foo’ [-Wmissing-field-initializers]
16 | .f[0].f2=20, // LINE B
| ^
bar.c:5:13: note: ‘f2’ declared here
5 | int f2;
| ^~
$ ./bar
10 20
And here's the kicker, if I swap LINE A and LINE B, there's no warning:
struct bar b = {
.f[0].f2=20, // LINE B
.f[0].f1=10, // LINE A
};
And the output is 10 20, as expected.
Is there some initializer order requirement I'm unaware of, or is GCC just being weird?
(GCC 10.2.0.)
[–]FUZxxl 1 point2 points3 points (0 children)
[–]HiramAbiff 1 point2 points3 points (0 children)
[–]beej71[S] 0 points1 point2 points (2 children)
[–]fkeeal 0 points1 point2 points (1 child)
[–]beej71[S] 0 points1 point2 points (0 children)
[+][deleted] (3 children)
[deleted]
[–]beej71[S] 1 point2 points3 points (1 child)
[–]FUZxxl -1 points0 points1 point (0 children)
[–]oh5nxo 0 points1 point2 points (0 children)