문제

In C99 (and not in C++), it's possible to initialize structs using this syntax:

struct info
{
    char    name[8+1];
    int     sz;
    int     typ;
};

struct info  arr[] =
{
    [0] = { .sz = 20, .name = "abc" },
    [9] = { .sz = -1, .name = "" }
};

What happens to the unspecified fields?

도움이 되었습니까?

해결책

They are zeroed. From the C99 standard §6.7.8 (Initialization)/21,

If there are fewer initializers in a brace-enclosed list than there are elements or members of an aggregate, or fewer characters in a string literal used to initialize an array of known size than there are elements in the array, the remainder of the aggregate shall be initialized implicitly the same as objects that have static storage duration.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top