I have a union typedef containing an anonymous struct. When trying to initialize it I always get a warning with gcc 4.9.2 (-Wall -Wextra -pedantic) about missing initializers for fields. However it seems to work as expected. Is there something wrong with the way the variable is initialized below?
#include <stdio.h>
typedef union {
unsigned int raw;
struct {
unsigned short hue;
unsigned char saturation;
unsigned char value;
};
} HSV;
int main() {
HSV color = {.hue = 1, .saturation = 2, .value = 4};
printf("hue: %d, saturation: %d, value: %d, raw: %08X\n", color.hue, color.saturation, color.value, color.raw);
}
When compiling gcc gives some warnings.
$ gcc -Wall -Wextra -pedantic -std=gnu11 main.c
main.c: In function 'main':
main.c:13:2: warning: missing initializer for field 'saturation' of 'struct <anonymous>' [-Wmissing-field-initializers]
HSV color = {.hue = 1, .saturation = 2, .value = 4};
^
main.c:7:20: note: 'saturation' declared here
unsigned char saturation;
^
main.c:13:2: warning: missing initializer for field 'value' of 'struct <anonymous>' [-Wmissing-field-initializers]
HSV color = {.hue = 1, .saturation = 2, .value = 4};
^
main.c:8:20: note: 'value' declared here
unsigned char value;
^
$ ./a.out
hue: 1, saturation: 2, value: 4, raw: 04020001