c - 点 (.) 在结构初始值设定项中是什么意思?

标签 c c99 designated-initializer

static struct fuse_oprations hello_oper = {
  .getattr = hello_getattr,
  .readdir = hello_readdir,
  .open    = hello_open,
  .read    = hello_read,
};

我不太了解这个 C 语法。我什至无法搜索,因为我不知道语法的名称。那是什么?

最佳答案

这是一项 C99 功能,允许您在初始化程序中按名称设置结构的特定字段。在此之前,初始化程序只需要按顺序包含所有字段的值——当然,这仍然有效。

所以对于以下结构:

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

...你可以使用

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

...或者:

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

...甚至:

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

...尽管最后两个仅适用于 C99。

关于c - 点 (.) 在结构初始值设定项中是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43672231/

相关文章:

iphone - 为 UITableViewController 子类重写哪个初始化器

objective-c - 指定初始化器,请澄清。

c - 结构体指针赋值

c - 在 mex 代码中释放内存

c - 使用 minGW 编译器设置 eclipse 以使用 C99

c - 当我将 std=c99 标志添加到 gcc 时,fileno、F_LOCK 和 F_ULOCK 变得未声明和不可用

c++ - 尝试使用 Cygwin b20 在 Windows 上编译 C 程序时出错

c - 使用双指针创建函数进行矩阵运算

c - for循环声明匿名结构,clang编译失败

ios - loadNibNamed 与 initWithFrame 两难设置框架的高度和宽度