复合字面量
发表于|更新于|分类8 (C语言)
1.C99标准引入的新特性,它允许我们在不定义变量的情况下,直接使用字面量来初始化一个对象。复合字面量的语法形式为:(type){initializer}
。
2.type
是一个类型名,initializer
是一个初始化器,它可以是一个常量、一个表达式或者一个初始化列表。复合字面量可以用于数组、结构体、联合体等类型的初始化。
3.例如,(int[\]){1, 2, 3}
表示一个匿名的整型数组,它的元素分别为1、2、3。(struct point){.x=1, .y=2}
表示一个匿名的结构体变量,它有两个成员变量x和y,分别被初始化为1和2。
- The type name shall specify an object type or an array of unknown size, but not a variable length array type.
type name指定了数组类型或结构体类型,数组长度不能是可变的。 - No initializer shall attempt to provide a value for an object not contained within the entire unnamed object specified by the compound literal.
匿名"对象"的初始化必须在在复合字面量的大括号中。 - If the compound literal occurs outside the body of a function, the initializer list shall consist of constant expressions.
如果复合字面量是文件作用域,initializer list的表达式必须是常量表达式。
#include <stdint.h> |
文章作者: Jamth
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 Jamth!
相关推荐