简体   繁体   English

struct 中变量名前的点是什么意思?

[英]What does a dot before the variable name in struct mean?

looking at the linux kernel source, I found this:查看linux内核源代码,我发现了这个:

static struct tty_operations serial_ops = {
  .open = tiny_open,
  .close = tiny_close,
  .write = tiny_write,
  .write_room = tiny_write_room,
  .set_termios = tiny_set_termios,
};

I've never seen such a notation in C. Why is there a dot before the variable name?我从未在 C 中看到过这样的符号。为什么变量名前有一个点?

This is a Designated Initializer , which is syntax added for C99.这是一个Designated Initializer ,它是为 C99 添加的语法。 Relevant excerpt:相关摘录:

In a structure initializer, specify the name of a field to initialize with '.fieldname =' before the element value.在结构体初始值设定项中,在元素值之前使用“.fieldname =”指定要初始化的字段的名称。 For example, given the following structure,例如,给定以下结构,

struct point { int x, y; }; 

the following initialization下面的初始化

struct point p = { .y = yvalue, .x = xvalue }; 

is equivalent to相当于

struct point p = { xvalue, yvalue };

It's sometimes called "designated initialization".它有时被称为“指定的初始化”。 This is a C99 addition, though it's been a GNU extension for a while.这是 C99 的补充,虽然它已经成为 GNU 扩展有一段时间了。

In the list, each .在列表中,每个. names a member of the struct to initialize, the so called designator.命名要初始化的结构成员,即所谓的指示符。

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

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