What does dot (.) mean in a struct initializer?

人盡茶涼 提交于 2019-11-26 14:12:24

This is a C99 feature that allows you to set specific fields of the struct by name in an initializer. Before this, the initializer needed to contain just the values, for all fields, in order -- which still works, of course.

So for the following struct:

struct demo_s {
  int     first;
  int     second;
  int     third;
};

...you can use

struct demo_s demo = { 1, 2, 3 };

...or:

struct demo_s demo = { .first = 1, .second = 2, .third = 3 };

...or even:

struct demo_s demo = { .first = 1, .third = 3, .second = 2 };

...though the last two are for C99 only.

These are C99's designated initializers.

Its known as designated initialisation (see Designated Initializers). An "initializer-list", Each '.' is a "designator" which in this case names a particular member of the 'fuse_oprations' struct to initialize for the object designated by the 'hello_oper' identifier.

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