1 variable pointer
The pointer to a variable is the address of the variable. The variable used to store the variable address is called a pointer variable.
1.1 pointer variable definition
A pointer variable is a variable used to store an address. It is defined as a pointer type in C language. The pointer variable is also a variable, but it does not store the value of the variable, but the address.
The general format for defining pointer variables is:
Type identifier * pointer variable name;
For example:
Int * pa, * pb;
Float * q;
Note: 1 This statement only defines pointer variables and does not explicitly declare a specific variable.
2. The "*" in front of the pointer variable indicates that the type of the variable is pointer-type. Therefore, the variable name is real pa and pb instead of * pa and * pb;
1.2 assignment and reference of pointer Variables
Like normal variables, pointer variables only make sense after being assigned values. pointer variables store the address of variables, so users are not allowed to assign constant values to them. Generally, you can assign values to pointer variables by using the address operator and address assignment.
The get address operator "&" can be added before the variables and array elements to obtain their memory address. Because the pointer variable is also a variable, the operator can also be added before the pointer variable. For example:
Int n1, n2;
Int * p1, * p2, * p3;
Float x, * p4, y;
P1 = & n1;
P2 = & n2;
P3 = p2;
We can also see that the pointer variable value assignment method is:
Pointer variable name = & variable name;
Pointer variable name = another pointer variable that has been assigned a value;
The pointer variable type must be consistent with the variable type it stores, but one exception is that the C language allows null pointers, that is, a pointer with a value of 0, it indicates that the pointer has no point and is often used to determine whether the function of the returned pointer is successful.
When the pointer variable points to the basic variable, you can use the indirect access method to access the data. In this case, you can use the content operator "*" to access the data, this operator is used to access the variables it points to through pointer variables. The basic format is:
* Pointer variable
Once the address of the variable is assigned to the pointer variable, * the pointer name and variable name are equivalent. Unless you change the pointer to the variable, changes to the pointer will affect the value of the variable. For example:
Int x, y1, * pi;
Double d;
Pi = & x;
X = 1;
/* Execute the following statement */
Y1 = * pi + 9;
Actually equivalent to: y1 = x + 9;
The following code uses pointer variables:
# Include "stdio. h"
Main (){
Int num = 12, * pt;/* defines a pointer variable pt pointing to int type data */
Float pi = 3.14, * pf;/* defines a pointer variable pf pointing to float data */
Char ch = 'M', * pc;/* defines a pointer variable pc pointing to char data */
Pt = #/* obtain the address of the variable num and assign it to pt */
Pf = π/* Get the pi address of the variable and assign it to pf */
Pc = & ch;/* obtain the address of the variable ch and assign it to pc */
Printf ("num = % d, * pt = % d \ n", num, * pt );
Printf ("pi = % 4.2f, * pf = % 4.2f \ n", pi, * pf );
Printf ("ch = % c, * pc = % c \ n", ch, * pc );
}
Pointer exchange:
# Include "stdio. h"
Main (){
Int a, B;
Int * p1, * p2, * p;
P1 = & a; p2 = & B;
Scanf ("% d", p1, p2 );
If (a <B ){
P = p1; p1 = p2; p2 = p;}/* the address is not the content in the address */
Printf ("the max is % d. \ n", * p1 );
}
Content exchange:
# Include "stdio. h"
Main (){
Int a, B, t;
Int * p1, * p2;
P1 = & a; p2 = & B;
Scanf ("% d", & a, & B );
If (a <B ){
T = * p1; * p1 = * p2; * p2 = t;}/* the content in the address is not the address */
Printf ("the max is % d. \ n", * p1 );
}
2 pointer variable operation
The pointer is the address, and the pointer variable can also perform some operations.
2.1 & and * operations
& Is the address operator used to obtain the address of a variable.
* It is a value operator and the function is to take the memory content of the address saved by the pointer variable.
For example:
Int;
Int * p;
P = &;
In this case, * p and a are the same, indicating the same variable. In short, * and & are reciprocal operations.
2.2 + and -- operation
In C, pointers can also perform auto-addition and auto-subtraction operations and addition and subtraction with integers. The pointer operation is different from the Integer Operation. It depends on the variable size executed by the pointer.
In pointer applications, it is often possible to combine the pointer plus or minus 1 with the * operator, for example:
* P ++ means to access the data pointed to by the pointer p, and then move the pointer variable to the position of an element.
* (P ++) indicates that the pointer variable moves the position of an element and then accesses the element.
If p points to the integer variable a, the following conclusions are available:
1 (* p) ++ is equivalent to a ++;
2 * (p ++) and * p ++ both get the value of variable a, but p no longer points to;
3 * p + 1 is equivalent to a + 1;
4 (* p) ++ and * p ++ are different.
2.3 comparison Operators
Generally, when two pointer variables of the same type are correctly assigned values, you can compare them. The relationships between pointer variables of the same type include:>, <, >=, <=, =, and ,! =. For example, compare two pointers of the same type. If they are equal, they point to the same address.
In comparison, the high address is greater than the low address.
Comparison between pointers pointing to different data types is meaningless. However, a pointer can perform an equal or unequal relational operation with NULL (0) to determine whether the pointer is NULL.
2.4 Subtraction
C language runs two pointer variables of the same type for subtraction. The operation result is the number of elements between two pointers. In fact, the difference between the two pointer addresses is actually divided by the length of the data type.
Code example of pointer operation:
# Include "stdio. h"
Main (){
Int a = 10, B = 30, * p1 = & a, * p2 = & B;/* define the pointer variable and initialize the pointer variable */
Printf ("% d % x
% X \ n ", a, B, * p1, * p2, p1, p2, & p1, & p2);/* output data, compare the relationship between observed data */
A ++; (* p2) ++;/* compare the auto-increment operation of the variable to which the Pointer Points */
Printf ("% d % x
% X \ n ", a, B, * p1, * p2, p1, p2, & p1, & p2 );
* P1 ++; * p2 --;/* comparison between the self-addition (subtraction) operation and the priority of the pointing operation */
Printf ("% d % x
% X \ n ", a, B, * p1, * p2, p1, p2, & p1, & p2 );
A = 123; * p2 = 99; B = 88;/* assign a value to the pointer variable */
Printf ("% d % x
% X \ n ", a, B, * p1, * p2, p1, p2, & p1, & p2 );
P1 ++; p2 ++;/* changes in the data value and address after the address is changed */
Printf ("% d % x
% X \ n ", a, B, * p1, * p2, p1, p2, & p1, & p2 );
Printf ("% d \ n", p1-p2, P2-P1);/* subtraction of pointer variables */
}
3. pointer variables are used as function parameters.
In the function section, we know that the return statement can be used to return the function value. In this way, only one data is returned, but the pointer can be used to achieve indirect access to the data, and the pointer is used as the function parameter, you can modify multiple values after the result is returned.
Example: call a function to calculate the values of two numbers plus and subtract, and print them in the main function.
# Include "stdio. h"
Void fun (int x, int y, int * pa, int * ps) {/* two number exchanges, the form parameter is a pointer */
Int add = 0, sub = 0;
* Pa = x + y;
* Ps = x-y;}/* pointer content change */
Main (){
Int a, B, add = 0, sub = 0;
Scanf ("% d", & a, & B );
Printf ("a = % d, B = % d \ n", a, B );
Fun (a, B, & add, & sub );
Printf ("% d + % d = % d \ n", a, B, add );
Printf ("% d-% d = % d \ n", a, B, sub );}
When using pointers as function parameters, you need to know how to correctly use pointer variables in calling functions to change the value of parameters. For example, you need to exchange the values of two data items, there are three methods:
3.1 exchange with common variables as parameters:
Void swap1 (int x, int y ){
Int temp;
Temp = x, x = y, y = temp;
}
This call only exchanges data in the function. When the function call ends and is returned to the main function, the memory of the form parameter is released and is not exchanged.
3.2 Use Pointer variables as parameters for data exchange:
Void swap1 (int * p1, int * p2 ){
Int * p;
P = p1, p1 = p2; p2 = p;
}
This kind of exchange also does not implement the function. After the call, it only implements the exchange of two pointers, but does not implement the real data exchange.
3.3 content pointed to by the Exchange pointer variable:
Void swid (int * p1, int * p2 ){
Int temp;
Temp = * p1, * p1 = * p2, * p2 = temp;
}
In this way, the real data exchange is realized.
4 pointer and one-dimensional array
An array is a set of data of the same type. Each element in the array occupies a continuous storage unit in the memory, and each memory unit has a corresponding address. The first address of the memory unit in the array is called the array pointer, and the first address of the memory unit in the array element is called the pointer of the array element. The array pointer and the array element pointer are two different concepts.
4.1 pointer to one-dimensional array elements
The array name without square brackets is the pointer to the array. You can assign the array name or the address of the first element to the pointer. For example, the following statements are valid:
Int a [5], * p, * q;
P = a; // Save the first address of the array
Q = & a [3]; // Save the address of the fourth element in the array
Because the array name represents the first address of the one-dimensional array, that is, the address of 0th elements, the following two statements are equivalent:
P =;
P = & a [0];
4.2 Access one-dimensional array elements with pointers
After the Pointer Points to the array, it establishes a connection between the pointer and the data. You can use pointers to access each element of the array. You can also use subscript to access array elements. However, using pointer access can reduce the memory usage and increase the running speed. The following code uses pointers to implement input and output of array elements:
# Include "stdio. h"
Main (){
Int arr [10], * pa = arr, I;
Printf ("Input 10 numbers :");
For (I = 0; I <10; I ++)
Scanf ("% d", pa + I);/* Use Pointer variables to input values of array elements */
Printf ("array [10]:");
For (I = 0; I <10; I ++)
Printf ("% d", * (pa + I);/* use the pointer variable pointing to the array to output the array */
Printf ("\ n ");
}
Since the pointer is a variable, you can directly obtain the element address through the address operation during Element Processing, and then access the data element. The above code can also be written as follows:
# Include "stdio. h"
Main (){
Int arr [10], * pa = arr;
Printf ("Input 10 numbers :");
For (; pa <arr + 10; pa ++)
Scanf ("% d", pa);/* Use Pointer variables to input values of array elements */
Printf ("array [10]:");
Pa = arr;
For (; pa <arr + 10; pa ++)
Printf ("% d", * pa);/* use the pointer variable pointing to the array to output the array */
Printf ("\ n ");
}
This access method also becomes a pointer method to access elements in the array, which can improve the program quality. When using pointers, pay special attention to the pointer's out-of-bounds problem, and pay attention to the changes in Pointer variables.
Example: Use Pointer variables to count the positive number of input data.
# Include "stdio. h"
Main (){
Int a [10];
Int I, * p, count = 0;
For (I = 0; I <10; I ++)
Scanf ("% d", a + I );
Printf ("\ n ");
For (p = a; p <a + 10; p ++) {/* the pointer can be moved 10 times */
If (* p> 0 ){
Count ++;
Printf ("% d", * p );
If (count % 4 = 0) printf ("\ n ");
}
}
}
From letthinking's column