1. pointer Concept
The address of the variable is called the pointer of the variable, and the variable storing the pointer is called the pointer variable.
A pointer variable is a variable that stores the address values of other variables. For example
Int I;
Int * pi = & I;
The pi variable value is the address of variable I.
* Is added before the pointer to indicate the memory unit pointed to by the pointer. For example
Int I, j;
Int * pi;
Pi = & I;
I = 9;
J = * pi;
Then the j and I values are the same.
A pointer variable is a 4-Byte variable. For any type of pointer variable, on the Win32 platform, it occupies 4 bytes of memory, for example
Int * pi;
Char * pch;
Double * pf
WORD * pw;
Then sizeof (pi) = sizeof (pch) = sizeof (pf) = sizeof (pw)
The value of the pointer variable does not change the value of the variable it originally points. For example
Int I, j;
Int * pi;
I = 8;
Pi = & I;
J = 9;
Pi = & j;
The I value is always 8.
Generally, you cannot assign a constant value to a pointer. To prevent Uninitialized pointers from being called,
The pointer Initialization is not NULL;
Int * pi = NULL; (correct)
Pi = (int *) 0x30405060; (not suitable)
Pointers can be used as function parameters, such
Void Swap (int * pi1, int * pi2)
{
Int iTemp = * pi1;
* Pi1 = * pi2;
* Pi2 = iTemp;
}
Int I, j;
I = 1;
J = 2;
Swap (& I, & j );
Analyze the following functions
Void Swap (int * pi1, int * pi2)
{
Int * iTemp = pi1;
Pi1 = pi2;
Pi2 = iTemp;
}
Pointer to the pointer. The pointer to a pointer is also a pointer variable, which also occupies 4 bytes, But it stores the value of another pointer address, which is represented by **, such
Int iData;
Int * piData;
Int ** ppiData;
IData = 100;
PiData = & iData;
PpiData = & piData;
Cout <** ppiData <endl;
2. pointer operation
Add/subtract an integer n from the pointer variable. The obtained value is still a pointer, indicating that the pointer moves n elements forward/backward, as shown in figure
Int I = 0x0f30348a;
Int * pi;
Char * pch = (char *) (& I );
Char * pch2 = pch + 1;
Pi = (int *) pch + 1;
Two pointer variables subtract, indicating the number of elements between two pointers. If the two pointer variables subtract, the pointer variables must be of the same type, as shown in figure
Int I = 0x0f30348a;
Char * pch = (char *) (& I );
Char * pch2 = pch + 1;
Int iEleCount = pch2-pch;
Adding the two pointers has no practical significance.
The comparison of the two pointers indicates the comparison of the addresses of the variables pointed to by the two pointers. The two pointers must be of the same type. For example
Char character = 'a ';
Char ch2 = 'B ';
Char * p;=& bytes;
Char * pch2 = & ch2;
Cout <(p0000> pch2) <endl;
3. Reference
A reference is an alias for a variable name. The use of the reference is equivalent to the use of the original variable. When declaring a referenced variable, it must be initialized. After initialization, the reference cannot be referenced as another variable, as shown in
Int I, j;
Int & iAlias = I;
& IAlias = j; (error)
Example 1:
Int a = 10;
Int & B =;
A = a *;
Cout <a <endl;
Cout <B <endl;
Cout <& B <endl;
Example 2:
Void Swap (int & a, int & B)
{
Int iTemp =;
A = B;
B = iTemp;
}
The difference between the reference Declaration and the address operator. When there is a type identifier before, it must be a declaration of reference, such as int & I, in addition to the variable address, such as cout <& I;
1 2 3 4