This is the 8th day of my participation in the Novembermore Challenge.The final text challenge in 2021
First, the concept of Pointers
1.1 Variables and Addresses
- Int a, char CH, and float num are all declared variables
- Addresses: In a computer, memory is divided into small chunks, each of which has a number called an address.
- Variables are usually stored in memory. And each memory has a unique address, and that address is the pointer
- If memory is compared to a hotel, when declaring a variable (int A), it is equivalent to checking in at the hotel desk. The front desk will give you a key card and a number, which simply means the address.
Pointers to variables and pointer variables
A pointer is the address of a variable, and a variable that stores the address of another variable is a pointer variable.
2.1 definition and use of pointer variables
(1) Definition of pointer variable
A pointer variable is defined as *, and three pointer variables are defined as follows. Their variable names are PI, pj, pf, not * PI, *pj, *pf. The * sign is used here only as a declaration.
Int I, j; int I, j; float f; Int * PI, *pj; float *pf;Copy the code
(2) Use of pointer variables
- Ampersand: The function of the ampersand operator is to fetch the address of the operation object. Constants, expressions, and register variables cannot be addressed because they are not stored in some memory location, but in registers, which have no address.
- Pointer operators (indirection operators): unary operator **”“** is used to access the storage unit based on the address value of the operation object. And ampersand are the inverse of each other.
Int I = 10; Printf ("%d", &i); printf("%d", &i); Int * PI = &i; int * PI = &i; Printf ("%d", * PI);Copy the code
Note: In C, all variable declarations must come first, but some compilers can pass without putting them first. Note here
(3) the combination direction of the, & and * operators
The ampersand and * operators have the same precedence, but are combined from right to left. You can think of it as starting from the right
// declare a variable I int I = 10; Int * PI = &i; int * PI = &i; Printf ("%d", &* PI);Copy the code
The above code defines a pointer variable PI to I, and the address of output I uses “&pi”. First, PI is a pointer variable, and the content of PI is the address of I. Since the operator is right-associative, PI is the first operation. That’s what’s in the PI address, which is 10. And then take the address, and PI is the address of I.
2.2 initialization of pointer variables
void main(){ int a = 10; Pa = pa; pa = pa; pa = pa; pa = pa; pa = pa; }Copy the code
2.3 pointer operation
(1) Assignment operation
void main(){ int *px, *py, *pz, x; //1, point to an address px = &x; //2; Pz = 4000; pz = 4000; }Copy the code
(2) pointer and integer addition and subtraction operations
- Pointer variable increment or decrement, that is, the pointer moves forward or backward by one storage unit
- The pointer is moved forward or backward by a specified location of storage with an integer added there
(3) Relational operation
- Px < py, check whether the address px points to is less than the address py points to
- Px == py, check whether px and py refer to the same address
- Px == 0 and px! = 0 indicates whether px is null
Here’s a little exercise:
Void invert(int *a, int start, int end); /** invert(int *a, int start); /** invert(int *a, int start); /** invert(int *a, int start); /** invert(int *a, int start); /** invert(int *a, int start); Int end){// A temporary variable used to exchange int temp; If (start < end){temp = a[start]; if(start < end){temp = a[start]; a[start] = a[end]; a[end] = temp; Invert (a, start + 1, end-1); // Invert (a, start + 1, end-1); }}Copy the code
Pointers and arrays
3.1. Pointers to arrays
- The array name is the first address of the array, a is an array, a = &a[0].
- Array elements can be accessed through Pointers,A = a [0],(a + 1) = a[1].
- Array names cannot operate on Pointers, such as the pointer p++ is legal, but the array a++ is illegal.
3.2 character Pointers and character arrays
In C, the system itself does not provide a string data type, but you can store a string in two ways: as a character array and as a character pointer.
(1) Character array mode
That’s the way we usually do it
Void main(){// define a string array char sentence[] = "Do not go gengle into that good night! ; printf("%s", sentence); }Copy the code
Where sentence is the beginning address of the character array.
(2) character pointer to achieve string
void main(){ char *sentence = "Do not go gentle into that good night!" ; printf("%s", sentence); }Copy the code
Here’s an exercise:
/** * copy */ void mian(){char *sentence = "Do not go gentle into that good night!" , copy[50]; int i; // Loop for(I = 0; sentence[i] ! = '\ 0'; Copy [I] = sentence[I]; } printf(" copy: %s", copy); }Copy the code
3.3, multilevel Pointers and pointer arrays
(1) Multi-level pointer
A pointer variable, as a variable, also has its own storage space. This storage space also has an address:
Void main(){// define a common variable int a = 10; // define a pointer variable to a int *p = &a; Int **pp = &p; int **pp = &p; Printf (" pa = %d\n", p); Printf (" secondary pointer PPA: %d", pp); Printf ("p stores: %d", *pp); }Copy the code
P = pp (p = pp); p = pp (p = pp); p = pp (p = pp); p = pp (p = pp);
(2) Pointer array
An array of Pointers is defined as follows:
int *a[10];
Copy the code
Get familiar with pointer arrays with an exercise explained in the comments:
Void main(){// Define and initialize an int array int a[5] = {1, 3, 5, 6, 8}, I; Int *p[5]; int *p[5]; for(i = 0; i < 5; i++){ p[i] = &a[i]; } // Define a secondary pointer to hold the first address of the pointer array. Int **pp = p; int **pp = p; For (I = 0; i < 5; I++){// the 0th element of array a is p, and the address of p is pp, so **pp = a[0] // the first element of array a is p + 1, and the address of p + 1 is pp + 1, so **(pp + 1) = a[1] // and so on. **(pp + n) = a[n] printf("%d\t", **(pp + i)); }}Copy the code
3.4 Pointers and Multidimensional Arrays
(1) Address of multidimensional array
Given a two-dimensional array A [4][2], the array can be understood in two dimensions.
- First remove [2] and look only at “A [4]”. At this point, A is just an ordinary one-dimensional array, and the following [2] only determines the nature of the elements of array A
- In array A, there are four elements, one of which we take to analyze the second dimension. The first element is a[0], which we treat as a whole, not as an array element, but as a name X. The second dimension can then be viewed as X[2], which is an array of two elements.
- The first address of the array X is the name of the array, that is, X. X is actually a[0], and by analogy X1, X2, etc., are a[1], a[2]. The first dimension of the array is filled with addresses, the addresses of each element X.
(2) Pointers to multidimensional arrays
Let me give you an example
Void mian () {/ / to create a common two-dimensional array int num [5] [5] = {{1, 3, 4, 5, 6}, {4, 5, 7, 8, 8}, {6, 8, 9, 0, 1}, {3, 4, 2, 1, 2}, {4, 5, 6, 3, 2}}; Int *p_num[5]; int i, j; Num [0][0], num[1][0], for(I = 0; i < 5; i++){ p_num[i] = &num[i][0]; } for(I = 0; i < 5; I ++){printf(" this is round %d array \n", I +1); for(j = 0; j < 5; J++) {/ / will p_num first address [I] as an array, and the contents of the array is stored for * (p_num + j) printf (" % d \ t ", * (p_num [I] + j)); } printf("\n"); }}Copy the code