char a1[] = {'C', '+', '+'}; // list init, no nullchar a2[] = {'C', '+', '+', '\0'}; // list init, explicit nullchar a3[] = "C++"; // null terminator added automaticallyconst char a4[3] = "C++"; // error: no space for the null!
Complicated Array Declarations
from right to left, from inside out.
int *ptrs[10]; // ptrs is an array of ten pointersint &refs[10] = /* ? */ // error; reference is not objectint (*Parr)[10] = &arr; // Parr is a pointer to an array of ten intsint (&Rarr)[10] = arr; // Rarr is a reference to …
说明数组名 arr 并不是一个指针,因为指向数组的指针不是 int** 而是 int (*)[10]. 并且声明引用时,也是直接拿数组名过来用。
Pointers and Arrays
when we use an array, the compiler ordinarily converts the array to a pointer to the first element. 🔗Type Conversions
Like when we subscript an array, we really subscript a pointer.
So…
Attention
No Copy or Assignment.
Guess when (is) assign(ed) to (by) array, we actually use it, so it convert to a pointer.
Mis Deduced by auto.
int a[] = {0, 1, 2};auto pa(a); // pa is an int* that point to the first element in adecltype(a) b = {0, 1}; // b is an array with 0, 1, 0auto &ra = a; // ra is a reference to an array of 3 ints