結構體和聯合體初始化
當初始化結構體或聯合體型別的物件時,初始化器必須是非空的(直到 C23)、用花括號括起來的、逗號分隔的成員初始化器列表。
= { 表示式 , ... } |
(1) | (直到 C99) | |||||||
= { 指示符(可選) 表示式 , ... } |
(2) | (C99 起) | |||||||
= { }
|
(3) | (自 C23 起) | |||||||
其中指示符是由形式為.
成員的單個成員指示符和形式為[
索引 ]
的陣列指示符組成的序列(用空格分隔或相鄰)。
所有未顯式初始化的成員都進行空初始化。
目錄 |
[編輯] 解釋
當初始化聯合體時,初始化器列表必須只有一個成員,該成員初始化聯合體的第一個成員,除非使用指定初始化器(C99 起)。
union { int x; char c[4]; } u = {1}, // makes u.x active with value 1 u2 = { .c={'\1'} }; // makes u2.c active with value {'\1','\0','\0','\0'}
當初始化結構體時,列表中的第一個初始化器初始化第一個宣告的成員(除非指定了指示符)(C99 起),並且所有後續沒有指示符的(C99 起)初始化器初始化在前一個表示式初始化之後宣告的結構體成員。
struct point {double x,y,z;} p = {1.2, 1.3}; // p.x=1.2, p.y=1.3, p.z=0.0 div_t answer = {.quot = 2, .rem = -1 }; // order of elements in div_t may vary
指示符使後續初始化器初始化由指示符描述的結構體成員。初始化然後按照宣告的順序向前進行,從指示符描述的成員之後宣告的下一個成員開始。 struct {int sec,min,hour,day,mon,year;} z = {.day=31,12,2014,.sec=30,15,17}; // initializes z to {30,15,17,31,12,2014} |
(C99 起) |
提供比成員更多的初始化器是一個錯誤。
[編輯] 巢狀初始化
如果結構體或聯合體的成員是陣列、結構體或聯合體,則花括號括起來的初始化器列表中的相應初始化器可以是適用於這些成員的任何有效初始化器,但其花括號可以按如下方式省略:
如果巢狀初始化器以左花括號開頭,則整個巢狀初始化器(直到其匹配的右花括號)初始化相應的成員物件。每個左花括號建立一個新的當前物件。當前物件的成員按其自然順序初始化,除非使用指示符(C99 起):陣列元素按下標順序,結構體成員按宣告順序,聯合體的第一個宣告成員。當前物件中未被右花括號顯式初始化的子物件進行空初始化。
struct example { struct addr_t { uint32_t port; } addr; union { uint8_t a8[4]; uint16_t a16[2]; } in_u; }; struct example ex = { // start of initializer list for struct example { // start of initializer list for ex.addr 80 // initialized struct's only member }, // end of initializer list for ex.addr { // start of initializer-list for ex.in_u {127,0,0,1} // initializes first element of the union } };
如果巢狀初始化器不以左花括號開頭,則從列表中只取足夠的初始化器來解釋成員陣列、結構體或聯合體的元素或成員;任何剩餘的初始化器將用於初始化下一個結構體成員。
struct example ex = {80, 127, 0, 0, 1}; // 80 initializes ex.addr.port // 127 initializes ex.in_u.a8[0] // 0 initializes ex.in_u.a8[1] // 0 initializes ex.in_u.a8[2] // 1 initializes ex.in_u.a8[3]
當指示符巢狀時,成員的指示符遵循包圍的結構體/聯合體/陣列的指示符。在任何巢狀的帶括號的初始化器列表中,最外層的指示符引用當前物件,並且只選擇當前物件中要初始化的子物件。 struct example ex2 = { // current object is ex2, designators are for members of example .in_u.a8[0]=127, 0, 0, 1, .addr=80}; struct example ex3 = {80, .in_u={ // changes current object to the union ex.in_u 127, .a8[2]=1 // this designator refers to the member of in_u } }; 如果任何子物件被顯式初始化兩次(當使用指示符時可能會發生),則列表中後面出現的初始化器是使用的那個(較早的初始化器可能不會被評估)。 儘管任何未初始化的子物件都會被隱式初始化,但如果同一個子物件的顯式初始化器在初始化器列表中較早出現,則隱式初始化永遠不會覆蓋它(選擇 clang 以觀察正確輸出)。 執行此程式碼 #include <stdio.h> typedef struct { int k; int l; int a[2]; } T; typedef struct { int i; T t; } S; T x = {.l = 43, .k = 42, .a[1] = 19, .a[0] = 18 }; // x initialized to {42, 43, {18, 19} } int main(void) { S l = { 1, // initializes l.i to 1 .t = x, // initializes l.t to {42, 43, {18, 19} } .t.l = 41, // changes l.t to {42, 41, {18, 19} } .t.a[1] = 17 // changes l.t to {42, 41, {18, 17} } }; printf("l.t.k is %d\n", l.t.k); // .t = x sets l.t.k to 42 explicitly // .t.l = 41 would zero out l.t.k implicitly } 輸出 l.t.k is 42 然而,當初始化器以左花括號開頭時,其當前物件會完全重新初始化,並且其任何子物件的任何先前的顯式初始化器都會被忽略。 struct fred { char s[4]; int n; }; struct fred x[ ] = { { { "abc" }, 1 }, // inits x[0] to { {'a','b','c','\0'}, 1 } [0].s[0] = 'q' // changes x[0] to { {'q','b','c','\0'}, 1 } }; struct fred y[ ] = { { { "abc" }, 1 }, // inits y[0] to { {'a','b','c','\0'}, 1 } [0] = { // current object is now the entire y[0] object .s[0] = 'q' } // replaces y[0] with { {'q','\0','\0','\0'}, 0 } }; |
(C99 起) |
[編輯] 注意
初始化器列表可以有一個尾隨逗號,該逗號會被忽略。
struct {double x,y;} p = {1.0, 2.0, // trailing comma OK };
在 C 中,帶花括號的初始化器列表不能為空(請注意 C++ 允許空列表,並且 C 中的結構體不能為空)。 |
(直至 C23) |
在 C 中,初始化器列表可以為空,就像在 C++ 中一樣。 |
(自 C23 起) |
struct {int n;} s = {0}; // OK struct {int n;} s = {}; // Error until C23: initializer-list cannot be empty // OK since C23: s.n is initialized to 0 struct {} s = {}; // Error: struct cannot be empty
初始化器列表中的每個表示式都必須是常量表達式,當初始化任何儲存期的聚合時。 |
(直到 C99) |
與所有其他初始化一樣,當初始化具有靜態或執行緒區域性(C11 起)儲存期的聚合時,初始化器列表中的每個表示式都必須是常量表達式。 static struct {char* p} s = {malloc(1)}; // error 任何初始化器中子表示式的求值順序是未確定序列的(但自 C++11 起在 C++ 中不是)。 int n = 1; struct {int x,y;} p = {n++, n++}; // unspecified, but well-defined behavior: // n is incremented twice in arbitrary order // p equal {1,2} and {2,1} are both valid |
(C99 起) |
[編輯] 示例
本節不完整 原因:更實用的例子,也許可以初始化一些套接字結構體。 |
#include <stdio.h> #include <time.h> int main(void) { char buff[70]; // designated initializers simplify the use of structs whose // order of members is unspecified struct tm my_time = { .tm_year=2012-1900, .tm_mon=9, .tm_mday=9, .tm_hour=8, .tm_min=10, .tm_sec=20 }; strftime(buff, sizeof buff, "%A %c", &my_time); puts(buff); }
可能的輸出
Sunday Sun Oct 9 08:10:20 2012
[編輯] 參考資料
- C17 標準 (ISO/IEC 9899:2018)
- 6.7.9/12-39 初始化 (p: 101-105)
- C11 標準 (ISO/IEC 9899:2011)
- 6.7.9/12-38 初始化 (p: 140-144)
- C99 標準 (ISO/IEC 9899:1999)
- 6.7.8/12-38 初始化 (p: 126-130)
- C89/C90 標準 (ISO/IEC 9899:1990)
- 6.5.7 初始化
[編輯] 另請參閱
C++ 文件,關於聚合初始化
|