非常教程

C参考手册

C 语法

compound literals

构造一个指定类型的未命名对象,当仅需要一次数组,结构或联合类型的变量时使用。

句法

( type ) { initializer-list }

(since C99)

其中

type

-

a type name specifying any complete object type or an array of unknown size, but not a VLA

initializer-list

-

list of initializers suitable for initialization of an object of type

说明

复合文字表达式构造由 type 指定的类型的未命名对象,并按 initializer-list 的指定进行初始化。

复合文字的类型是 type(除非 type 是一个未知大小的数组;其大小是从初始化程序列表中推导出来的,与数组初始化中一样)。

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

如果复合文字出现在块范围(在这种情况下对象的生存期在封闭块的末尾结束),则复合文字评估的未命名对象具有静态存储持续时间,前提是复合文字出现在文件范围和自动存储持续时间。

注意

常量限定字符或宽字符数组类型的复合文字可能与字符串文字共享存储空间。

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

每个复合文字在其范围内仅创建一个对象:

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
}

因为复合文字是未命名的,所以复合文字不能自我引用(一个已命名的结构可以包含一个指向自身的指针)。

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

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
 
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
 
    struct point {double x,y;};
    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 
              (struct point){.x=3, .y=4}); // and calls drawline1, passing them by value
    drawline2(&(struct point){.x=1, .y=1},  // creates two structs with block scope 
              &(struct point){.x=3, .y=4}); // and calls drawline2, passing their addresses
}

参考

  • C11 standard (ISO/IEC 9899:2011):
    • 6.5.2.5 Compound literals (p: 85-87)
  • C99 standard (ISO/IEC 9899:1999):
    • 6.5.2.5 Compound literals (p: 75-77)

C 语法相关

1.#define directive
2.#elif directive
3.#else directive
4.#endif directive
5.#error directive
6.#if directive
7.#ifdef directive
8.#ifndef directive
9.#include directive
10.#line directive
11.#pragma directive
12.alignas
13.Alternative operators and tokens
14.Analyzability
15.Arithmetic operators
16.Arithmetic types
17.Array declaration
18.Array initialization
19.ASCII Chart
20.Assignment operators
21. types
22.Basic concepts
23.Bit fields
24.break statement
25.C language
26.C Operator Precedence
27.cast operator
28.character constant
29.Comments
30.Comparison operators
31.Conditional inclusion
32.Conformance
33.const type qualifier
34.Constant expressions
35.continue statement
36.Declarations
37.do-while loop
38.Enumerations
39.Escape sequences
40.Expressions
41.External and tentative definitions
42.File scope
43.floating constant
44.for loop
45.Function declarations
46.Function definitions
47.Functions
48.Generic selection
49.goto statement
50.Identifier
51.if statement
52.Implicit conversions
53.Increment/decrement operators
54.Initialization
55.inline function specifier
56.integer constant
57.Lifetime
58.Logical operators
59.Lookup and name spaces
60.Main function
61.Member access operators
62.Memory model
63.Objects and alignment
64.Order of evaluation
65.Other operators
66.Phases of translation
67.Pointer declaration
68.Preprocessor
69.restrict type qualifier
70.return statement
71.Scalar initialization
72.Scope
73.sizeof operator
74.Statements
75.static assert declaration
76.Static storage duration
77.Storage-class specifiers
78.string literals
79.Struct and union initialization
80.Struct declaration
81.switch statement
82.Thread storage duration
83.Type
84.Type
85.Typedef declaration
86.Undefined behavior
87.Union declaration
88.Value categories
89.Variadic arguments
90.volatile type qualifier
91.while loop
92._Alignof operator
93._Noreturn function specifier
C

C 语言是一门通用计算机编程语言,应用广泛。C 语言的设计目标是提供一种能以简易的方式编译、处理低级存储器、产生少量的机器码以及不需要任何运行环境支持便能运行的编程语言。