What happens to fields not named by a designated initializer?

独自空忆成欢 提交于 2019-11-27 14:48:18

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/3374446/what-happens-to-fields-not-named-by-a-designated-initializer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!