int (*Parray)[10] = &arr; // Parray points to an array of ten ints int (&arrRef)[10] = arr; // arrRef refers to an array of ten ints <pre name="code" class="cpp">int *(&arry)[10] = ptrs; // arry is a reference to an array of ten pointers
-
When we use a variable to subscript an array, we normally should define thatvariable to have typesize_t.
The size_t type is defined in thecstddefheader.
-
One such implication is that when we use an array as aninitializer for a variable defined usingauto, the deduced type is apointer, not an array:
int ia[] = {0,1,2,3,4,5,6,7,8,9}; // ia is an array of ten ints auto ia2(ia); // ia2 is an int* that points to the first element in ia
It is worth noting that this conversion does not happen when we usedecltype. The type returned bydecltype(ia)is array of ten ints:
decltype(ia) ia3 = {0,1,2,3,4,5,6,7,8,9};
<pre name="code" class="cpp">ia3 = p; // error: can't assign an int* to an array ia3[4] = i; // ok: assigns the value of i to an element in ia3
-
There is, however, astring member functionnamed c_str that we can often use to accomplish what we want:
char *str = s; // error: can't initialize a char* from a string
const char *str = s.c_str(); // ok
If a program needs continuing access to the contents of the array returnedbystr(), the program must copy the array returned byc_str.
-
To use a multidimensional array in a range for, the loop control variable for all but the innermost array must be references.
3.5Arrays & 3.6Multidimentional Arrays
最新推荐文章于 2021-10-29 18:58:46 发布