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

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

问题描述

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,};

<块引用>

此声明使用标准 C标记结构初始化语法.

谁能详细说明一下?

解决方案

当您在传统"ANSI C 语言 (C89/90) 中使用聚合初始值设定项({} 中的初始值设定项)时,您必须按顺序为每个结构成员提供一个单独的初始化程序,从第一个开始.例如

struct S { int a, b, c, d;};结构 S s = { 1, 2, 3, 4 };/* 1 表示 `s.a`,2 表示 `s.b`,依此类推... */

您不需要为所有成员指定初始化程序,即您可以随时停止(其余成员将被零初始化).

如果由于某种原因您只关心显式初始化结构的第三个成员,则必须为第一个和第二个成员提供虚拟"显式初始化器(只是为了获得所需的第三个)

/* 我们只关心显式初始化 `s.c` */结构 S s = { 0, 0, 3 };/* 但我们也必须显式初始化 `s.a` 和 `s.b` */

或完全放弃特定的初始化(可能用通用的 = { 0 } 替换它)并使用后续分配给特定成员

struct S s = { 0 };s.c = 3;

这种基于赋值的方法的一个显着优点是它独立于 struct S 声明中成员 c 的位置.

C 语言 (C99) 的新规范允许您通过在 {}

中提供所需的成员名称来使用标记"初始化程序

struct S s = { .c = 3 };

那样你只显式初始化所需的成员(并让编译器对其余成员进行零初始化).这不仅为您节省了一些输入,而且使聚合初始值设定项独立于结构类型声明中指定成员的顺序.

聚合初始值设定项,您可能知道,也可以与数组一起使用.C99 也支持使用数组进行标记"初始化.以下示例说明了标签"在数组情况下的外观

int a[10] = { [5] = 3 };/* `a[5]` 被初始化为 3,`a` 的其余部分被零初始化 */

再次值得注意的是,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 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 };
/* but we have to explicitly initialize `s.a` and `s.b` as well */

or abandon specific initialization entirely (likely replacing it with generic = { 0 }) and use a subsequent assignment to specific members

struct S s = { 0 };
s.c = 3;

One notable benefit of this assignment-based approach is that it is independent from the position of member c in the declaration of struct S.

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 have the compiler to 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 specified in the struct type declaration.

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 };
/* `a[5]` is initialized with 3, the rest of `a` is zero-initialized */

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 gets initialized, and the members without explicit initializers get zero-initialized.

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

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