Pointer Introduction
Pointer is a widely used data type in C language. Using pointer programming is one of the most important C language styles. Pointer variables can be used to represent various data structures; arrays and strings can be easily used; memory addresses can be processed like assembly languages to produce refined and efficient programs. Pointers greatly enrich the functions of the C language. Learning pointer is the most important part in learning C language. Whether or not we can correctly understand and use pointer is a sign of understanding C language. At the same time, pointers are also the most difficult part of C language. In addition to understanding the basic concepts correctly, you must also perform more programming and perform machine debugging. As long as you do this, it is not difficult to grasp the pointer.
The basic concept of pointer is in the computer, and all data is stored in the memory. Generally, a byte in memory is called a memory unit. The number of memory units occupied by different data types varies, for example, the integer value occupies 2 units and the character value occupies 1 unit, the second chapter provides a detailed introduction. To access these memory units correctly, the number must be assigned to each memory unit. You can find the memory unit accurately based on the number of a memory unit. The memory unit number is also called the address. Since the memory unit can be found based on the number or address of the memory unit, this address is usually called a pointer. Memory Unit pointers and memory unit content are two different concepts. A common example can be used to illustrate the relationship between them. When we go to the bank for deposit and withdrawal, the bank staff will go to our deposit form based on our account, find the deposit and withdrawal amount on the deposit form. Here, the account is the pointer to the deposit, and the number of deposits is the content of the deposit. For a memory unit, the address of the Unit is a pointer, and the data stored in the unit is the content of the unit. In C, a variable can be used to store pointers. Such variables are called pointer variables. Therefore, the value of a pointer variable is the address of a memory unit or the pointer of a memory unit. In the figure, there is a character variable C, whose content is "K" (ASCII code is 75 in decimal number), and C occupies the 011a unit (the address is expressed in hexadecimal number ). There is a pointer Variable P with the content of 011a. In this case, P points to variable C, or P points to variable C. Strictly speaking, a pointer is an address and a constant. However, a pointer variable can be assigned different pointer values. However, pointer variables are often referred to as pointers. To avoid confusion, we agree that "Pointer" refers to an address, a constant, and "pointer variable" refers to a variable with a value of an address. The purpose of defining pointers is to access memory units through pointers.
Since the value of the pointer variable is an address, this address can be not only the address of the variable, but also the address of other data structures. Store one in a pointer variable
Array or the first address of a function? Because arrays or functions are stored consecutively. The first address of the array or function is obtained by accessing the pointer variable, and the array or function is found. In this way, a pointer variable can be used to represent the function where an array appears, as long as the first address of the array or function is assigned to the pointer variable. By doing so, the program concept will be very clear, and the program itself will be refined and efficient. In C, a data type or data structure usually occupies a group of continuous memory units. The concept of "Address" cannot describe a data type or structure well. Although "Pointer" is actually an address, it is the first address of a data structure, it refers to a data structure, so the concept is clearer and the representation is clearer. This is also an important reason for introducing the "Pointer" concept.
Pointer variable type description
The pointer variable type description includes three parts:
(1) pointer type description, that is, defining a variable as a pointer variable;
(2) pointer variable name;
(3) Data Type of the variable pointed to by the variable value (pointer.
The general form is: type specifier * variable name;
Where, * indicates that this is a pointer variable, and the variable name is the defined pointer variable name. The type specifier indicates the Data Type of the variable pointed to by the pointer variable.
For example, int * P1 indicates that P1 is a pointer variable and its value is the address of an integer variable. Or P1 points to an integer variable. Which integer variable P1 points to is determined by the address assigned to P1.
Another example is:
Staic int * P2;/* P2 is the pointer variable pointing to the static integer variable */
Float * P3;/* P3 is the pointer variable pointing to the floating point variable */
Char * P4;/* p4 is the pointer variable pointing to the character variable */It should be noted that a pointer variable can only point to the same type of variable, for example, P3 can only point to the floating point variable, it cannot point to a floating-point variable or a character variable at any time.
Pointer variable assignment
Like normal variables, pointer variables must be defined and specified before use. Pointer variables that are not assigned values cannot be used. Otherwise, the system may be confused or even crashed. The pointer variable value can only be assigned to the address, and no other data can be assigned to it. Otherwise, an error will occur. In C language, the variable address is allocated by the compilation system and is completely transparent to users. users do not know the specific address of the variable. The C language provides the address operator & to represent the address of the variable. The general form is: & variable name; for example, & A indicates the address of variable A, and & B indicates the address of variable B. The variables must be described in advance. There is a pointer Variable P pointing to an integer variable. To assign the address of integer variable A to P, you can use either of the following methods:
(1) method int A of pointer variable initialization;
Int * P = &;
(2) method of the value assignment statement int;
Int * P;
P = &;
It is not allowed to assign a number to the pointer variable, so the following value assignment is incorrect: int * P; P = 1000; the "*" operator cannot be added before the pointer variable to be assigned, for example, writing * P = & A is also incorrect.
Pointer variable operation
Pointer variables can perform some operations, but the types of operations are limited. It can only perform value assignment and some arithmetic operations and relational operations.
1. pointer Operators
(1) obtain the address operator &
The bitwise operator & is a single object operator. Its combination is from right to left. Its function is to take the address of a variable. In scanf function and pointer variable assignment, we understand and use the & operator.
(2) obtain the content operator *
The content operator * is a single object operator, and its combination is from right to left, used to indicate the variable referred to by the pointer variable. The variable followed by the * operator must be a pointer variable. Note that the pointer operator * is not the same as the pointer operator * in the pointer variable description. In pointer variable description, "*" is a type specifier, indicating that the variable after it is a pointer type. The "*" in the expression is an operator used to represent the variable referred to by the pointer variable.
Main (){
Int A = 5, * P = &;
Printf ("% d", * P );
}
......
The pointer Variable P obtains the address of integer variable. This statement indicates the value of output variable.
2. pointer variable operation
(1) Value assignment
The assignment of pointer variables can be performed in the following forms:
① Pointer variable initialization value assignment, which has been described earlier.
② Assign the address of a variable to the pointer variable pointing to the same data type. For example:
Int A, * pA;
Pa = & A;/* assign the address of integer variable A to the integer pointer variable Pa */
③ Assign the value of a pointer variable to another pointer variable of the same type. For example:
Int A, * pA = & A, * pb;
PB = PA;/* assign the address of a to the pointer variable Pb */
Since both PA and Pb are pointer variables pointing to integer variables, values can be assigned to each other.
④ Assign the first address of the array to the pointer variable pointing to the array.
Example: int A [5], * pA;
Pa = A; (array name indicates the first address of the array, so you can assign the pointer variable Pa to the array)
It can also be written as follows:
Pa = & A [0];/* the address of the first element of the array is also the first address of the entire array,
You can also assign Pa *. If you want to access the non-first element of an array through a pointer, for example, to access a [2], use * (PA + 2 ), instead of * (PA + 8), this compiler will automatically fix the issue and does not need to calculate the number of bytes */
Of course, you can also initialize the value assignment method:
Int A [5], * pA =;
⑤ Assign the first address of the string to the pointer variable pointing to the character type. For example, char * PC; Pc = "C Language"; or use the initialization value assignment method to write it as char * Pc = "C Language "; it should be noted that it is not to load the entire string into pointer variables, but to load the first address of the character array storing the string into pointer variables. We will also introduce it in detail later.
6. Assign the function entry address to the pointer variable pointing to the function. For example, INT (* PF) (); pF = f;/* F indicates the function name */
(2) addition and subtraction arithmetic operations
For pointer variables pointing to an array, you can add or subtract an integer n. If Pa is a pointer variable pointing to array A, the PA + N, pa-N, PA ++, ++ pa, pa --, and -- pa operations are legal. Adding or subtracting an integer n to a pointer variable means moving the pointer to the current position (pointing to an array element) forward or backward n positions. It should be noted that the array pointer variable moves forward or backward a position and the address plus 1 or minus 1 is conceptually different. Because Arrays can have different types, array elements of different types occupy different bytes. For example, if you add 1 to the pointer variable, that is, move one position backward to indicate that the pointer variable points to the first address of the next data element. Instead of adding 1 to the original address.
For example:
Int A [5], * pA;
Pa = A;/* pA points to array A, which also points to a [0] */
Pa = pa + 2;/* pA points to a [2], that is, the value of PA is & pa [2] */addition and subtraction of pointer variables can only be performed on Array pointer variables, adding or subtracting pointer variables pointing to other types of variables is meaningless. (3) The operation between two pointer variables can only be performed between two pointer variables pointing to the same array. Otherwise, the operation is meaningless.
① Subtract two pointer Variables
The difference between two pointer variables is the number of elements that differ between the elements of the array referred to by the two pointers. The difference between the two pointer values (addresses) is actually divided by the length (number of bytes) of the array element ). For example, pf1 and pf2 are two pointer variables pointing to the same floating point number group. If pf1 is set to 2010 H and pf2 is set to 2000 h, each element in the floating point group occupies four bytes, therefore, the result of the pf1-pf2 is (2000 h-2010 h)/4 = 4, indicating the difference between pf1 and pf2 4 elements. Two pointer variables cannot be added. For example, what does pf1 + pf2 mean? It is meaningless.
② Relational calculation of two pointer Variables
The relationship between two pointer variables pointing to the same Array indicates the relationship between the elements of the array they refer. For example:
Pf1 = pf2 indicates that pf1 and pf2 point to the same array element.
Pf1> pf2 indicates that pf1 is in the high address location.
Pf1 <pf2 indicates that pf2 is in the low address location
Main (){
Int A = 10, B = 20, S, T, * pA, * pb;
Pa = &;
PB = & B;
S = * pa + * pb;
T = * pA ** Pb;
Printf ("A = % d/Nb = % d/Na + B = % d/na * B = % d/N", A, B, A + B, A * B );
Printf ("s = % d/Nt = % d/N", S, T );
}
......
Description: Pa, PB is an integer pointer variable
Assign a value to pointer variable Pa, and PA points to variable.
Assign a value to the pointer variable Pb, which points to variable B.
The meaning of this line is to calculate the sum of A + B (* pa is A, * pb is B ).
This row is the product of a * B.
Output result.
Output result.
......
The pointer variable can also be compared with 0. If P is set to a pointer variable, P = 0 indicates that p is a null pointer and does not point to any variable. p! = 0 indicates that P is not a null pointer. Null pointers are obtained by assigning 0 values to pointer variables. For example: # define null 0 int * P = NULL; it is different to assign 0 values to the pointer variable and not assign values. When the pointer variable is not assigned a value, it can be any value and cannot be used. Otherwise, unexpected errors may occur. The pointer variable can be used after it is assigned a value of 0, but it does not point to a specific variable.
Main (){
Int A, B, C, * Pmax, * pmin;
Printf ("input three numbers:/N ");
Scanf ("% d", & A, & B, & C );
If (A> B ){
Pmax = &;
Pmin = & B ;}
Else {
Pmax = & B;
Pmin = & ;}
If (C> * Pmax) Pmax = & C;
If (C <* pmin) pmin = & C;
Printf ("max = % d/Nmin = % d/N", * Pmax, * pmin );
}
......
Pmax and pmin are integer pointer variables.
Enter the prompt.
Enter three numbers.
If the first digit is greater than the second digit...
Pointer variable assignment
Pointer variable assignment
Pointer variable assignment
Pointer variable assignment
Judge and assign values
Judge and assign values
Output result
......
Description and use of array pointer Variables
The pointer variable pointing to an array is called an array pointer variable. Before discussing the description and use of array pointer variables, we should clarify several relationships.
An array is composed of consecutive memory units. The array name is the first address of this continuous memory unit. An array is composed of various array elements (subscript variables. Each array element occupies several consecutive memory units based on its type. The first address of an array element is the first address of several memory units it occupies. A pointer variable can point to either an array or an array element. You can assign the array name or the address of the first element to it. To point a pointer variable to element I, You can assign the first address of element I to it or assign the array name and I to it.
There is a real array A and the pointer variable pointing to a Is Pa. From Figure 6.3, we can see the following relationships:
Pa, A, & A [0] all point to the same unit. They are the first address of array A and the first address of element a [0. PA + 1, A + 1, & A [1] Point to element a [1]. And a + I, A + I, & A [I]
Point to element a [I]. It should be noted that PA is a variable, while a and & A [I] are constants. Pay attention to programming.
Main (){
Int A [5], I;
For (I = 0; I <5; I ++ ){
A [I] = I;
Printf ("A [% d] = % d/N", I, a [I]);
}
Printf ("/N ");
}
Main Function
Define an integer array and an integer variable
Loop statement
Assign values to Arrays
Print the value of each array
......
Output line feed
......
The following table describes the array pointer variables:
Type specifier * pointer variable name
The type description indicates the type of the array. From the general form, we can see that the pointer variable pointing to the array is the same as the pointer variable pointing to the common variable.
After the pointer variable is introduced, you can access the array elements in two ways.
The first method is the subscript method, that is, accessing array elements in the form of a [I. This method is used in Chapter 4 to introduce arrays.
The second method is the pointer method, which uses the * (PA + I) form to access array elements through indirect access.
Main (){
Int A [5], I, * pA;
Pa =;
For (I = 0; I <5; I ++ ){
* PA = I;
Pa ++;
}
Pa =;
For (I = 0; I <5; I ++ ){
Printf ("A [% d] = % d/N", I, * pA );
Pa ++;
}
}
Main Function
Define integer arrays and pointers
Point pointer Pa to array
Loop
Assign the value of variable I to the array unit of a [] directed by the pointer Pa.
Point pointer Pa to the next unit of [].
......
Pointer PA re-obtains the first address of array
Loop
Outputs all elements in array a in an array.
Point pointer Pa to the next unit of [].
......
......
Next, let's take another example. This example is the same as the previous example, but the implementation method is different.
Main (){
Int A [5], I, * pA =;
For (I = 0; I <5 ;){
* PA = I;
Printf ("A [% d] = % d/N", I ++, * pA ++ );
}
}
Main Function
Define an integer array and pointer, and point the pointer to array
Loop
Assign the value of variable I to the array unit of a [] directed by the pointer Pa.
Outputs all elements in array a using pointers, and points pointer Pa to the next unit of [].
......
......