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

发布于 2024-12-14 06:15:02 字数 224 浏览 3 评论 0原文

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

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

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. I can't even search because I don't know the syntax's name. What's that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

流绪微梦 2024-12-21 06:15:02

这是一项 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。

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.

人│生佛魔见 2024-12-21 06:15:02

这些是 C99 的指定初始化器

These are C99's designated initializers.

上课铃就是安魂曲 2024-12-21 06:15:02

它被称为指定初始化(请参阅指定初始化器)。一个“初始化列表”,每个“.”都是一个
指示符”在本例中命名了一个特定成员
'fuse_oprations' 结构体,用于为指定的对象进行初始化
'hello_oper' 标识符。

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.

病毒体 2024-12-21 06:15:02

整个语法被称为指定初始化程序,正如 COD3BOY 已经提到的那样,当您需要在声明时将结构初始化为某些特定值或默认值时,通常会使用它。

The whole syntax is known as designated initializer as already mentioned by COD3BOY and it is used in general when you need to initialize your structure at the time of declaration to some specific or default values.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文