Is an empty initializer list valid C code? -
it common use {0}
initialize struct
or array
consider case when first field isn't scalar type. if first field of struct person
struct
or array, line result in error (error: missing braces around initializer
).
struct person person = {0};
at least gcc allows me use empty initializer list accomplish same thing
struct person person = {};
but valid c code?
also: line guaranteed give same behavior, i.e. zero-initialized struct
?
struct person person;
no, empty initializer list not allowed. can shown gcc when compiling -std=c99 -pedantic
:
a.c:4: warning: iso c forbids empty initializer braces
the reason way grammar defined in §6.7.9 of 2011 iso c standard:
initializer: assignment-expression { initializer-list } { initializer-list , } initializer-list: designation(opt) initializer initializer-list , designation(opt) initializer
according definition, initializer-list must contain @ least 1 initializer.
Comments
Post a Comment