什么是点(。)在一个struct初始化是什么意思? [英] What does dot (.) mean in a struct initializer?

查看:144
本文介绍了什么是点(。)在一个struct初始化是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我不明白这个C语法很好。我甚至无法搜索,因为我不知道语法的名字。那是什么?

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?

推荐答案

这是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 };

...或者:

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

...甚至是:

...or even:

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

...虽然最后两个用于C99只。

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

这篇关于什么是点(。)在一个struct初始化是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆