Pointer is an important concept in C language. Correct and flexible use of pointer can effectively represent complex data structures, dynamically allocate memory, and conveniently use strings; the array can be effectively and conveniently used; more than one place value can be obtained when a function is called; memory address can be directly processed, which is necessary for designing system software.
Concepts of addresses and pointers
If a variable is defined in the program, allocate a memory unit to the variable during compilation. The system allocates a certain length of space based on the variable type defined in the program.
Each byte in the memory has a number, which is the "Address". The address of a variable is called "Pointer ". It means that the memory unit with the address can be found.
ThereforePointerIs variableAddress.
The variable that stores the variable address isPointer variableTo point to another variable.
In the program, use the "*" symbol to indicate "pointing ". For example, I _pointer represents the pointer variable, and * I _pointer is the variable pointed to by I _pointer.
Define a pointer variable
The general form of defining pointer variables is:Base type * pointer variable name
Why do we need to specify the base type? We know that the number of bytes occupied by integer data and real data in the memory is different. When moving pointers and performing pointer operations, if the pointer points to an integer variable, so moving a pointer to a position is to move two bytes. If the pointer points to a real variable, four bytes are moved to the position of the variable. Therefore, you must specify the type of the variable to which the pointer variable points, that is, the base type.
Pointer variable reference
- &: Obtain the address Operator
- *: Pointer operator (or "indirect access" operator)
Main (){ |
|
Int A, B;
|
|
Int * pointer_1, pointer_2;
|
Define two pointer variables. * It only indicates that they are pointer variables. |
A = 100; B = 10;
|
|
Pointer_1 = &;
|
|
Pointer_2 = & B;
|
|
Printf ("% d, % d/N", a, B );
|
|
Printf ("% d, % d/N", * pointer_1, * pointer_2 );
|
* Pointer_1 indicates the variable pointed to by pointer_1. |
} |
|
The running result is: |
|
100,10 |
|
100,10 |
|
Here:
* Pointer_1 =
Pointer_1 = &
* & A = & * pointer_1 =
(* Pointer_1) ++ is equivalent to a ++, and parentheses are required, because ++ and * are of the same priority level, while the combination direction is from right to left, therefore, removing the parentheses is equivalent to * (pointer_1 ++)
Pointer variables as function parameters
Sample Code:
Main (){ |
Int A = 0, B = 1;
|
Int * pointer_1, * pointer_2;
|
Pointer_1 = &;
|
Pointer_2 = & B;
|
Swap (); implements a and B Interchange
|
Printf ("% d, % d", a, B );
|
} |
The following provides several alternative swap Implementation Solutions. Let's analyze them one by one:
Swap (INT p1, p2 ){ |
Int temp;
|
Temp = p1;
|
P1 = P2;
|
P2 = temp;
|
} |
Analysis: because the function is called using the value transfer method, here the values of the local variables P1 and P2 are exchanged, and the values of A and B in the main function remain unchanged.
Swap (int * P1, int * P2 ){ |
Int * temp;
|
* Temp = * P1;
|
* P1 = * P2;
|
* P2 = * temp;
|
} |
Analysis: It seems feasible on the surface, but temp does not have a fixed address value. * The Memory Unit pointed by temp is unpredictable. Therefore, assigning a value to * temp may damage the normal operation of the system.
Swap (int * P1. * P2 ){ |
Int * P;
|
P = p1;
|
P1 = P2;
|
P2 = P;
|
} |
Analysis: only the P1 and P2 values of the local pointer variables are changed, and the values of A and B are not changed.
Swap (int * P1, * P2 ){ |
Int temp;
|
Temp = * P1;
|
* P1 = * P2;
|
* P2 = temp;
|
} |
Analysis: Correct. The operation is p1, p2 points to the address, that is, a, B.
Swap (Int & P1, & p2 ){ |
Int temp;
|
Temp = p1;
|
P1 = P2;
|
P2 = temp;
|
} |
Analysis: Correct. C ++ is only available. References are used. Several others are written as swap (& A, & B) during the call. Swap (A, B) is used for this call ).