我正在关注《C Primer Plus》这本书,遇到了这样一段代码:
// designate.c -- use designated initializers
#include <stdio.h>
#define MONTHS 12
int main(void)
{
int days[MONTHS] = {31, 28, [4] = 31, 30, 31, [1] = 29};
int i;
for (i = 0; i < MONTHS; i++)
printf("%2d %d\n", i+1, days[i]);
return 0;
}
当我编译它时它会报告错误:
$ cc designate.c
designate.c:6:57: warning: initializer overrides prior initialization of this subobject [-Winitializer-overrides]
int days[MONTHS] = {31, 28, [4] = 31, 30, 31, [1] = 29};
^~
designate.c:6:29: note: previous initialization is here
int days[MONTHS] = {31, 28, [4] = 31, 30, 31, [1] = 29};
^~
1 warning generated.
尽管如此,./a.out
工作正常。
$ ./a.out
1 31
2 29
3 0
4 0
5 31
6 30
7 31
8 0
9 0
10 0
11 0
12 0
我很困惑我应该从错误报告中学到什么?