什么是标记化的结构初始化语法? [英] What is tagged structure initialization syntax?

查看:111
本文介绍了什么是标记化的结构初始化语法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 结构的file_operations scull_fops = {
.owner = THIS_MODULE,
.llseek = scull_llseek,
.read = scull_read,
.WRITE = scull_write,
.ioctl = scull_ioctl,
。开= scull_open,
.release = scull_release,
};


  

此声明使用标准C
  标签的结构初始化
  语法。​​


有人能详细点吗?


解决方案

当您使用聚合初始化(初始化在 {} )中的传统的ANSI C语言( C89 / 90),必须以提供一个单独的初始化每个结构构件,与第一开始。例如:

 的struct {诠释A,B,C,D; };的struct S = {1,2,3,4};
/ * 1`s.a`,2`s.b`等等... * /

您不需要指定所有成员初始化,即你可以在任何时候停止(剩余成员将初始化为零)。如果由于某种原因,你只关心显式初始化结构的第三个成员,你没有别的选择,只能提供虚拟明确初始化为第一和第二部件(只是为了得到想要的三分之一)

  / *我们只在乎曾经明确初始化`s.c` * /
的struct S = {0,0,3};
/ *,我们别无选择,只能明确地初始化`s.a`和`s.b`以及* /

C语言(C99)的新规范,您可以通过内提供所需的成员名称中使用标记初始化的 {}

 的struct S = {.C = 3};

这样,你只有明确初始化所需的成员(S)(并要求编译器隐含零初始化的其余部分)。

这不仅为您节省一些打字,但也使得独立于其中的成员是在结构中声明的顺序初始化总量

总结初始化,你可能知道,可用于数组,太。和C99支持使用数组标记初始化为好。如何在标签看在是由下面的例子说明一个数组的情况下

  int类型的[10] = {[5] = 3};

值得注意的是,多一个是C语言继续坚持全有或全无的方式集合初始化时间:如果您指定只有一个明确的初始化(或某些)一个结构成员或数组时,整个聚合体(结构或数组)被初始化,并没有明确的初始化成员获得零初始化。

struct file_operations scull_fops = {
.owner = THIS_MODULE,
.llseek = scull_llseek,
.read = scull_read,
.write = scull_write,
.ioctl = scull_ioctl,
.open = scull_open,
.release = scull_release,
};

This declaration uses the standard C tagged structure initialization syntax.

Can someone elaborate?

解决方案

When you use aggregate initializers (initializers in {}) in the "traditional" ANSI C language (C89/90), you have to supply an individual initializer for each structure member in order, beginning with the first. For example

struct S { int a, b, c, d; };

struct S s = { 1, 2, 3, 4 };
/* 1 for `s.a`, 2 for `s.b` and so on... */

You are not required to specify initializers for all members, i.e. you can stop at any time (remaining members will be zero-initialized). If for some reason you only cared to explicitly initialize the third member of the structure, you had no other choice but to supply "dummy" explicit initializers for the first and the second members (just to get to the desired third)

/* We only care to explicitly initialize `s.c` */
struct S s = { 0, 0, 3 };
/* and we have no choice but to explicitly initialize `s.a` and `s.b` as well */

The new specification of C language (C99) allows you to use "tagged" initializers by supplying the desired member name within the {}

struct S s = { .c = 3 };

That way you only explicitly initialize the desired member(s) (and ask the compiler to implicitly zero-initialize the rest).

This not only saves you some typing but also makes the aggregate initializers independent from the order in which the members are declared in the struct.

Aggregate initializers, as you probably know, can be used with arrays, too. And C99 supports "tagged" initialization with arrays as well. How the "tags" look in case of an array is illustrated by the following example

int a[10] = { [5] = 3 };

It is worth noting one more time that C language continues to stick to the "all or nothing" approach to aggregate initialization: if you specify an explicit initializer for just one (or some) members of a struct or an array, the whole aggregate (struct or array) gets initialized, and the members without explicit initializers get zero-initialized.

这篇关于什么是标记化的结构初始化语法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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