Summary of this chapter
1. Pointers are an important part of the C language, and using pointer programming has the following advantages:
(1) Improve the compilation efficiency and execution speed of the program.
(2) The use of pointers can be used to share variables or data structures between the keynote function and the modulated function to facilitate two-way data communication.
(3) Dynamic storage allocation can be realized.
(4) Facilitate the presentation of various data structures and the preparation of high-quality procedures.
2. Operation of the pointer
(1) Address operator and: to find the address of the variable
(2) Content operator *: Represents the variable that the pointer refers to
(3) Assignment operation
• Assigning variable addresses to pointer variables
• Assigning values to the same type of pointer variables
• Assign the first address of the array, the string to the pointer variable
• Assign function entry address to pointer variable
(4) Addition and subtraction operation
For pointers to arrays, the pointer variables of a string can be added and reduced, such as p+n,p-n,p++,p--. You can subtract two pointer variables that point to the same array. It is meaningless to add and subtract operations that point to other types of pointer variables.
(5) Relational operation
Two pointer variables that point to the same array can be greater than, less than, or equal to comparison operations. The pointer can be compared to 0, and p==0 indicates that p is a null pointer.
3. The various descriptions and meanings related to pointers are shown in the table below.
int *p; P is a pointer variable that points to an integral type
int *p[n]; P is an array of pointers, consisting of n pointer elements that point to an integral type.
int (*p) [n]; P is a pointer variable that points to an integral two-dimensional array, and the number of columns in the two-dimensional array is n
int *p () p is a function that returns a pointer value that points to an integral type
Int (*p) () p is a pointer to a function that returns an integral type
int **p p is a pointer variable that points to another pointer that points to an integral type.
4. A lot of instructions about pointers are composed of pointers, arrays, and function descriptions.
But not any combination, for example, an array cannot be composed of functions, that is, an array element cannot be a function, nor can a function return an array or return another function. For example
int a[5] (); that's wrong.
5. About parentheses
When interpreting a combination descriptor, the square brackets and parentheses to the right of the identifier take precedence over the "*" number to the left of the identifier, and the brackets and parentheses are combined from left to right with the same precedence. However, you can change the binding order of the conventions with parentheses.
6. The rule for reading the combination specifier is "from the Inside Out."
Starting with an identifier, first look at the right side of the bracket or bracket, if there is a first explanation, and then look at the left there is no * number. If you encounter a closing parenthesis at any time, the contents of the parentheses must be processed with the same rules before proceeding. For example:
int* (* (*a) ()) [10]
↑↑↑↑↑↑↑
7 6 4 2 1 3 5
It gives an inward reading order, which is explained below:
(1) identifier A is described as;
(2) A pointer variable, which points to;
(3) A function, which returns;
(4) A pointer pointing to the pointer;
(5) An array of 10 elements, of a type;
(6) Pointer type, it points;
(7) int type data.
So a is a function pointer variable, and the function returns a pointer value that points to an array of pointers that point to the integral type.