命名空间
变体
操作

复合字面量 (since C99)

来自 cppreference.cn
< c‎ | language

就地构造指定类型的未命名对象(可以是结构体、联合体,甚至数组类型)。

目录

[编辑] 语法

( storage-class-specifiers (optional)(since C23) type ) { initializer-list } (自 C99 起)
( storage-class-specifiers (optional)(since C23) type ) { initializer-list , } (自 C99 起)
( storage-class-specifiers (optional) type ) { } (自 C23 起)

其中

storage-class-specifiers - (since C23) 存储类说明符列表,只能包含 constexpr, static, register, 或 thread_local
type - 指定任何完整对象类型或未知大小数组的类型名称,但不能是 VLA
initializer-list - 适用于初始化 type 对象的初始化器列表

[编辑] 解释

复合字面量表达式构造一个由 type 指定类型的未命名对象,并按照 initializer-list 的指定对其进行初始化。接受指定初始化器

复合字面量的类型是 type(除非 type 是未知大小的数组;其大小从 initializer-list 中推断,如数组初始化中所示)。

复合字面量的值类别是 左值(可以取其地址)。

复合字面量求值的未命名对象,如果复合字面量出现在文件作用域,则具有静态存储期;如果复合字面量出现在块作用域,则具有自动存储期(在这种情况下,对象的生命周期在封闭块的末尾结束)。 (直到 C23)
如果复合字面量在函数体外部和任何参数列表外部求值,则它与文件作用域关联;否则,它与封闭块关联。根据此关联,存储类说明符(可能为空)、类型名称和初始化列表(如果有)应使得它们对于文件作用域或块作用域中的对象定义是有效的说明符,分别采用以下形式:
   storage-class-specifiers typeof(type) ID = { initializer-list };
其中 ID 是整个程序唯一的标识符。复合字面量提供一个未命名对象,其值、类型、存储期和其他属性如同由上述定义语法给出;如果存储期是自动的,则未命名对象实例的生命周期是封闭块的当前执行。如果存储类说明符包含除 constexpr, static, register, 或 thread_local 之外的其他说明符,则行为未定义。
(自 C23 起)

[编辑] 备注

const 限定的字符或宽字符数组类型的复合字面量可以与字符串字面量共享存储。

(const char []){"abc"} == "abc" // might be 1 or 0, unspecified

每个复合字面量在其作用域内仅创建一个对象

int f (void)
{
    struct s {int i;} *p = 0, *q;
    int j = 0;
again:
    q = p, p = &((struct s){ j++ });
    if (j < 2) goto again; // note; if a loop were used, it would end scope here,
                           // which would terminate the lifetime of the compound literal
                           // leaving p as a dangling pointer
    return p == q && q->i == 1; // always returns 1
}

由于复合字面量是未命名的,因此复合字面量不能引用自身(命名的结构体可以包含指向自身的指针)

尽管复合字面量的语法类似于类型转换,但重要的区别在于类型转换是非左值表达式,而复合字面量是左值。

[编辑] 示例

#include <stdio.h>
 
int *p = (int[]){2, 4}; // creates an unnamed static array of type int[2]
                        // initializes the array to the values {2, 4}
                        // creates pointer p to point at the first element of
                        // the array
const float *pc = (const float []){1e0, 1e1, 1e2}; // read-only compound literal
 
struct point {double x,y;};
 
int main(void)
{
    int n = 2, *p = &n;
    p = (int [2]){*p}; // creates an unnamed automatic array of type int[2]
                       // initializes the first element to the value formerly
                       // held in *p
                       // initializes the second element to zero
                       // stores the address of the first element in p
 
    void drawline1(struct point from, struct point to);
    void drawline2(struct point *from, struct point *to);
    drawline1(
        (struct point){.x=1, .y=1},  // creates two structs with block scope and
        (struct point){.x=3, .y=4}); // calls drawline1, passing them by value
    drawline2(
        &(struct point){.x=1, .y=1},  // creates two structs with block scope and
        &(struct point){.x=3, .y=4}); // calls drawline2, passing their addresses
}
 
void drawline1(struct point from, struct point to)
{
    printf("drawline1: `from` @ %p {%.2f, %.2f}, `to` @ %p {%.2f, %.2f}\n",
        (void*)&from, from.x, from.y, (void*)&to, to.x, to.y);
}
 
void drawline2(struct point *from, struct point *to)
{
    printf("drawline2: `from` @ %p {%.2f, %.2f}, `to` @ %p {%.2f, %.2f}\n",
        (void*)from, from->x, from->y, (void*)to, to->x, to->y);
}

可能的输出

drawline1: `from` @ 0x7ffd24facea0 {1.00, 1.00}, `to` @ 0x7ffd24face90 {3.00, 4.00}
drawline2: `from` @ 0x7ffd24facec0 {1.00, 1.00}, `to` @ 0x7ffd24faced0 {3.00, 4.00}

[编辑] 参考

  • C23 标准 (ISO/IEC 9899:2024)
  • 6.5.2.5 复合字面量 (页码:待定)
  • C17 标准 (ISO/IEC 9899:2018)
  • 6.5.2.5 复合字面量 (页码:61-63)
  • C11 标准 (ISO/IEC 9899:2011)
  • 6.5.2.5 复合字面量 (页码:85-87)
  • C99 标准 (ISO/IEC 9899:1999)
  • 6.5.2.5 复合字面量 (页码:75-77)