Initialize
在 C++ 裡 Array 的 Size 要為 constant expression,也就是 expression 能在 compile time 就算出來。但因為 C 支援在 Array Size 塞變數,所以 G++ 將此寫法視為一個 extension,只要你不在 compile 時強調要遵循 C++ 的 Rule (-pedantic)。
unsigned cnt = 42; // not a constant expression
constexpr unsigned sz = 42; // constant expression
int arr[10]; // array of ten ints
int *parr[sz]; // array of 42 pointers to int
string bad[cnt]; // error: cnt is not a constant expression
string strs[get_size()]; // ok if get_size is constexpr, error otherwise
int a1[3] = {0, 1, 2}; // [0, 1, 2]
int a2[] = {0, 1, 2}; // [0, 1, 2]
int a3[5] = {0, 1, 2}; // [0, 1, 2, 0, 0]
string a4[3] = {"hi", "bye"}; // ["hi", "bye", ""]
賦予初始值可用 string 來直接 Initialize。
char a1[] = {'C', '+', '+'}; // list initialization, no null
char a2[] = {'C', '+', '+', '\0'}; // list initialization, explicit null
char a3[] = "C++"; // null terminator added automatically
const char a4[6] = "Daniel"; // error: no space for the null!
加入 pointer 和 reference 的宣告
int *ptrs[10]; // ptrs is an array of ten pointers to int
int &refs[10] = /* ? */; // error: no arrays of references
int (*Parray)[10] = &arr; // Parray points to an array of ten ints
int (&arrRef)[10] = arr; // arrRef refers to an array of ten ints
int *(&arry)[10] = ptrs; // arry is a reference to an array of ten pointers