1. Double pointer
(1) Difference between double pointer and ordinary single pointer
->In essence, the essence of double pointer and single pointer is pointer variable, and the essence of pointer variable is variable.
->Both the single pointer variable and the double pointer variable themselves occupy 4 bytes of memory space.
(2) The essence of double pointer
->A double pointer is essentially a pointer variable. The difference between a double pointer and an ordinary pointer is that the type of variable it points to must be a single pointer.
(3) Usage of double pointer:
The double pointer points to the address of the single pointer:
char a; char **p1; // double pointer char *p2; // Single pointer printf("sizeof(p1) = %d.\n", sizeof(p1)); //4 printf("sizeof(p2) = %d.\n", sizeof(p2)); //4 p2 = &a; //p1 = &a; // P1 is the char * * type, &a is the char * type. // Char * * type means that the variable pointed to by the pointer is of char * type // The char * type indicates that the variable pointed to by the pointer is of char type. p1 = &p2; // p2 itself is of char * type, and then the address is changed into char * * type, which is compatible with p1.
Double pointer to pointer array:
int *p1[5]; int **p3; p3 = p1; // p1 is the pointer array name, which is essentially the array name. The right value of the array name represents the first element of the array // First address. The elements of the array are of type int *, so the right value of p1 represents an int* // The address of the type variable, so p1 is the pointer to the pointer of an int type variable, so // It is a double pointer int * *;
In practical programming, sometimes in order to change an external pointer variable through the function, the address of the pointer variable (i.e. double pointer) will be passed in
void func(int **p) { *p = (int *)0x12345678; } int main(void) { int a = 4; int *p = &a; // p points to a printf("p = %p.\n", p); // p prints out the memory address of a func(&p); // Point p somewhere else inside func printf("p = %p.\n", p); // p doesn't point to a anymore, so it doesn't print out the address of A *p = 23; // Because p points to 0x12345678 at this time, but this address is not valid // Access is allowed, so a segment error will occur. return 0; }
2. Two dimensional array
(1) Memory image of 2D array:
a[0][0] a[0][1] a[0][4] a[1][0] a[1][1] a[1][4] b[0] b[1] b[4] b[5] b[6] b[9]
->One dimensional array is composed of multiple memory units continuously distributed in memory, while two-dimensional array is also composed of multiple memory units continuously distributed in memory.
->From a memory perspective, there is no essential difference between a one-dimensional array and a two-dimensional array. There is no essential difference between the two-dimensional array int a[2][5] and the one-dimensional array int b[10]. We can write down the correspondence between the two in the same unit.
->Two dimensional array a and one-dimensional array b are exactly the same in memory utilization efficiency and access efficiency (or the difference is negligible).
->In some cases, two-dimensional arrays are used instead of one-dimensional arrays, because two-dimensional arrays are easy to understand, code is easy to write and easy to organize (such as picture display).
(2) Which is the first dimension and which is the second dimension:
->In the two-dimensional array int a[2][5], 2 is the first dimension and 5 is the second dimension.
->The first dimension of a two-dimensional array is the outermost layer. The first dimension itself is an array, and the elements stored in this array are also a one-dimensional array; The second dimension of the two-dimensional array is the layer inside. The second dimension itself is a one-dimensional array. The elements stored in the array are ordinary elements. The second dimension of the one-dimensional array itself is stored in the first dimension of the two-dimensional array as elements.
(3) Subscript access and pointer access of two-dimensional array
->Review: there are two ways to access one-dimensional arrays:
with int b[10]take as an example, int *p = b;. b[0] Equivalent to *(p+0); b[9] Equivalent to *(p+9); b[i] Equivalent to *(p+i)
->There are two ways to access two-dimensional arrays:
with int a[2][5]For example,(Suitable type)p = a; a[0][0]Equivalent to*(*(p+0)+0); a[i][j]Equivalent to *(*(p+i)+j)
(4) Pointer to the array name of a two-dimensional array
->The array name of the two-dimensional array represents the first address of the first element (that is, the second-dimensional array) in the first dimensional array of the two-dimensional array, that is, the array pointer (the address of the array).
->The array name a of a two-dimensional array is equivalent to & A [0], which is consistent with the symbolic meaning of a one-dimensional array.
(5) The pointer points to the first dimension of the two-dimensional array
->Use int *p to point to the first dimension a[i] of a two-dimensional array, which is a one-dimensional pointer.
(6) The pointer points to the second dimension of the two-dimensional array
#include <stdio.h> int main(void) { int a[2][5] = {{1, 2, 3, 4, 5}, {6, 7, 8, 9, 10}}; printf("a[1][3] = %d.\n", a[1][3]); //9 printf("a[1][3] = %d.\n", *(*(a+1)+3)); //9 //int *p1 = a; // type mismatch //int **p2 = a; // If the type does not match, a warning will be reported // Pointer to the array name of a two-dimensional array int (*p3)[5]; // Array pointer. The pointer points to an array. The array has five int type elements p3 = a; // a is the array name of the two-dimensional array. As the right value, it represents the array of the first dimension of the two-dimensional array // The first address of the first element of, which is equivalent to & A [0] p3 = &a[0]; printf("a[0][3] = %d.\n", *(*(p3+0)+3)); //4 printf("a[1][4] = %d.\n", *(*(p3+1)+4)); //10 // The pointer points to the first dimension of the two-dimensional array //int *p4 = &a[0]; // may not int *p4 = a[0]; // a[0] represents the first element of the first dimension of the two-dimensional array, which is equivalent to // The array name of the overall array of the second dimension. The array name also represents the first element of the array // First address, so a[0] is equivalent to & a[0] [0]; int *p5 = &a[0][0]; printf("a[0][4] = %d.\n", *(p4+4)); //5 // Points to the second dimension of a two-dimensional array int *p6 = a[1]; printf("a[1][1] = %d.\n", *(p6+1)); //7 return 0; }