(5) C array nature
Arrays in C are an aggregation type. Elements of the same type are stored in order, that is, arrays. In C, the array type is defined by nesting.
Look at the memory storage format of the next two-dimensional array int array [2] [3] = {1, 2, 3}, {4, 5, 6:
Arrays in C are nested and defined: Elements in a two-dimensional array are one-dimensional arrays, and elements in a three-dimensional array are two-dimensional arrays ......
A two-dimensional array like array [2] [3] is composed of two one-dimensional arrays: array [0], array [1], the size of the two one-dimensional arrays is the second subscript 3 of array. Array [0] and array [1] are two one-dimensional array names. Because the elements are stored sequentially, you can access the elements in the array in one of the following ways:
Int main (void) {int array [2] [3] = {1, 2, 3}, {4, 5, 6 }}; // point P to the first element of the array int * P = & array [0] [0]; while (P <& array [1] [3]) {printf ("% 4D", * P); P ++;} printf ("\ n"); Return 0 ;}Run
The key to successful access is that the multi-dimensional array in C is essentially an array and the elements are stored in sequence.
The & array [1] [3] appears in the Code. Is it out of bounds? In theory, array [1] [2] is the last element of array. array [1] [3] is the next element. Here we take its address instead of accessing the element, this is acceptable.
Next we will discuss the types of pointers for these three Arrays: array, array [0], and array [1.
First, in the (3) array and pointer, we mentioned that for the array int array [N]; (N is a constant ). Array name array is a constant pointer to the first element of the array (that is, it cannot point to another storage unit), and & array is a pointer to the entire array. And so on:
- Array [0] is a pointer to array [0] [0]. The type is int *. & Array [0] is a pointer to an array and the type is int (*) [3].
- Array [1] and array [0] are of the same type. The types of & array [1] and & array [0] are the same. Of course, they only have the same type and different values.
- Array should be a pointer to array [0], while array [0] is a one-dimensional array int [3], so array is a pointer to a one-dimensional array, the type is int (*) [3]. & Array should be a pointer to the entire two-dimensional array, type: int (*) [2] [3].
Lab Verification:
int main(void){int array[2][3] = {{1, 2, 3},{4, 5, 6}};int *pa = array[0];int (*pb)[3] = &array[0];int (*pc)[3] = array;int (*pd)[2][3] = &array;return 0;}
Compilation, without any warning, our conjecture is correct. The above two experiments fully illustrate the nature of the C array:
Array.
Let's take a short test. The 3D array int array [2] [3] [4]; The type of array? & Array type? Array [0] type? Array [0] [0] type? (Think about the answer at the end, and then look at the answer .)
In another test, pointer operations are used for verification.
int main(void){int array[2][3] = {{1, 2, 3},{4, 5, 6}};int *pa = array[0];int (*pb)[3] = &array[0];int (*pc)[3] = array;int (*pd)[2][3] = &array;printf("array...%p\n", array);printf(" pa+1...%p\n", pa + 1);printf(" pb+1...%p\n", pb + 1);printf(" pc+1...%p\n", pc + 1);printf(" pd+1...%p\n", pd + 1);return 0;}Run
Calculate the difference between the last four and array, and draw a conclusion.
To sum up, the essence of array C is the array of arrays. For the array name array and the pointer type of & array, you only need to remember two points. These two points are:
- The array points to the first element of the array to find out what the element type is, And the pointer type is introduced naturally.
- & Array is specified to point to the entire array, and its type is simpler.
Test answer: the array type is int (*) [3] [4]; & the array type is int (*) [2] [3] [4]; the type of array [0] is int (*) [4]; The type of array [0] [0] is int *.
Column Directory: C pointer