?
This document uses PHP Chinese website manual Release
當初始化一個數(shù)組類型的對象時,初始化器必須是一個字符串文字(可選地用大括號括起來)或者是一個由數(shù)組成員初始化的大括號括起來的列表:
= string_literal | (1) | |
---|---|---|
= { expression , ... } = { designator(optional) expression , ... } | (2) | (until C99)(since C99) |
1)用于字符和寬字符數(shù)組的字符串字面初始值設(shè)定項
2)用逗號分隔的數(shù)組元素的初始值表達式列表,可選地使用形式為[
constant-expression 的數(shù)組指示符]
=
(自 C99開始)
已知大小的陣列和未知大小的陣列可能被初始化,但不是 VLA。(自 C99以來)。
所有未明確初始化的數(shù)組元素都是以與具有靜態(tài)存儲持續(xù)時間的對象相同的方式隱式初始化的。
字符串文字(可以用大括號括起來)可以用作匹配類型數(shù)組的初始值設(shè)定項:
普通字符串文字和 UTF-8字符串(因為 C11)可以初始化任何字符類型的陣列(char
,signed char
,unsigned char
)
帶前綴的寬字符串文字可用于初始化任何兼容的類型的數(shù)組(忽略 cv 資格) wchar_t
u-prefixed wide string literals can be used to initialize arrays of any type compatible with (ignoring cv-qualifications) char16_t U-prefixed wide string literals can be used to initialize arrays of any type compatible with (ignoring cv-qualifications char32_t | (since C11) |
---|
帶 u 前綴的寬字符串文字可用于初始化任何兼容類型的數(shù)組(忽略 cv 資格) char16_t
帶U前綴的寬字符串文字可以用來初始化任何兼容類型的數(shù)組(忽略 cv 資格) char32_t
(since C11)
字符串文字的連續(xù)字節(jié)或?qū)捵址淖值膶捵址òńK止空字節(jié)/字符)初始化數(shù)組的元素:
char str[] = "abc"; // str has type char[4] and holds 'a', 'b'. 'c', '\0'wchar_t wstr[4] = L"貓"; // str has type wchar_t[4] and holds L'貓', '\0', '\0', '\0'
如果數(shù)組的大小已知,則它可能比字符串文字的大小小1,在這種情況下,將終止空字符被忽略:
char str[3] = "abc"; // str has type char[3] and holds 'a', 'b', 'c'
請注意,與使用直接訪問字符串字面值不同,此數(shù)組的內(nèi)容是可修改的char* str = "abc";
。
當一個數(shù)組用初始化器的大括號初始化時,列表中的第一個初始化器初始化索引為0的數(shù)組元素(除非指定了指定符號)(自C99開始),并且每個后續(xù)的初始化器都沒有指定符(自C99開始)初始化索引1處的數(shù)組元素大于前一個初始化器初始化的數(shù)組元素。
int x[] = {1,2,3}; // x has type int[3] and holds 1,2,3int y[5] = {1,2,3}; // y has type int[5] and holds 1,2,3,0,0int z[3] = {0}; // z has type int[3] and holds all zeroes
在初始化已知大小的數(shù)組時,提供比元素更多的初始化程序是錯誤的(除了從字符串文字初始化字符數(shù)組時)。
A designator causes the following initializer to initialize of the array element described by the designator. Initialization then continues forward in order, beginning with the next element after the one described by the designator. int n5 = {4=5,0=1,2,3,4} // holds 1,2,3,4,5 int aMAX = { // starts initializing a0 = 1, a1 = 3, ... 1, 3, 5, 7, 9, MAX-5 = 8, 6, 4, 2, 0 } // for MAX=6, array holds 1,8,6,4,2,0 // for MAX=13, array holds 1,3,5,7,9,0,0,0,8,6,4,2,0 ("sparse array") | (since C99) |
---|
初始化未知大小的數(shù)組時,指定初始化程序的最大下標確定正在聲明的數(shù)組的大小。
如果數(shù)組的元素是數(shù)組,結(jié)構(gòu)體或聯(lián)合體,則大括號括起來的初始化程序中相應的初始化程序是對這些成員有效的任何初始化程序,除了它們的大括號可以省略如下:
如果嵌套的初始化程序以開始大括號開始,則整個嵌套初始化程序直到其大括號初始化相應的數(shù)組元素:
int y[4][3] = { // array of 4 arrays of 3 ints each (4x3 matrix) { 1 }, // row 0 initialized to {1, 0, 0} { 0, 1 }, // row 1 initialized to {0, 1, 0} { [2]=1 }, // row 2 initialized to {0, 0, 1}}; // row 3 initialized to {0, 0, 0}
如果嵌套的初始化程序不是以大括號開頭的話,那么只有列表中的足夠的初始化程序可以用來解釋子數(shù)組 struct struct 或 union 的元素或成員; 任何剩余的初始化器都將被初始化為下一個數(shù)組元素:
int y[4][3] = { // array of 4 arrays of 3 ints each (4x2 matrix)1, 3, 5, 2, 4, 6, 3, 5, 7 // row 0 initialized to {1, 3, 5}}; // row 1 initialized to {2, 4, 6} // row 2 initialized to {3, 5, 7} // row 3 initialized to {0, 0, 0} struct { int a[3], b; } w[] = { { 1 }, 2 }; // array of structs // { 1 } is taken to be a fully-braced initializer for element #0 of the array // that element is initialized to { {1, 0, 0}, 0} // 2 is taken to be the first initialized for element #1 of the array // that element is initialized { {2, 0, 0}, 0}
Array designators may be nested; the bracketed constant expression for nested arrays follows the bracketed constant expression for the outer array: int y4 = {0=1, 1=1, 2=1}; // row 0 initialized to {1, 0, 0} // row 1 initialized to {0, 1, 0} // row 2 initialized to {1, 0, 0} // row 3 initialized to {0, 0, 0} | (since C99) |
---|
數(shù)組初始值設(shè)定項中子表達式的求值順序在 C 中是不確定的(但在c ++ 11以后,不在 C ++中):
int n = 1;int a[2] = {n++, n++}; // unspecified, but well-defined behavior, // n is incremented twice (in arbitrary order) // a initialized to {1, 2} and to {2, 1} are both validputs((char[4]){'0'+n} + n++); // undefined behavior: // increment and read from n are unsequenced
在 C 中,初始化程序的支撐列表不能為空。C ++允許空列表:
int a[3] = {0}; // valid C and C++ way to zero-out a block-scope arrayint a[3] = {}; // invalid C but valid C++ way to zero-out a block-scope array
與所有其他初始化一樣,初始化器列表中的每個表達式在初始化靜態(tài)或線程本地存儲持續(xù)時間數(shù)組時都必須是常量表達式:
static char* p[2] = {malloc(1), malloc(2)}; // error
int main(void){ // The following four array declarations are the same short q1[4][3][2] = { { 1 }, { 2, 3 }, { 4, 5, 6 } }; short q2[4][3][2] = {1, 0, 0, 0, 0, 0, 2, 3, 0, 0, 0, 0, 4, 5, 6}; short q3[4][3][2] = { { { 1 }, }, { { 2, 3 }, }, { { 4, 5 }, { 6 }, } }; short q4[4][3][2] = {1, [1]=2, 3, [2]=4, 5, 6}; // Character names can be associated with enumeration constants // using arrays with designators: enum { RED, GREEN, BLUE }; const char *nm[] = { [RED] = "red", [GREEN] = "green", [BLUE] = "blue", };}
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):
3.5.7 / 12-初始化