简体   繁体   English

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

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

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

I don't understand this C syntax well.我不太了解这个 C 语法。 I can't even search because I don't know the syntax's name.我什至无法搜索,因为我不知道语法的名称。 What's that?那是什么?

This is a C99 feature that allows you to set specific fields of the struct by name in an initializer.这是一项 C99 功能,允许您在初始化程序中按名称设置结构的特定字段。 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. ...虽然最后两个仅适用于 C99。

这些是 C99 的指定初始值设定项

Its known as designated initialisation (see Designated Initializers ).它被称为designated initialisation (请参阅指定初始化程序)。 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. ' 是一个“ designator ”,在这种情况下,它命名了 ' fuse_oprations ' 结构的一个特定成员,以初始化由 ' hello_oper ' 标识符指定的对象。

整个语法被称为 COD3BOY 已经提到的指定初始化程序,它通常用于在声明时需要将结构初始化为某些特定或默认值的情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM