A pointer variable is a special variable that differs from other types of variables you have learned before: it is used to point to another variable. In order to represent the link between a pointer variable and the variable it points to, in C + + a "*" symbol is used to point to, for example, I_pointer is a pointer variable, and *i_pointer represents the variable that i_pointer points to.
The following two statements function the same:
Defining pointer Variables
C + + Specifies that all variables must be defined before they are used, that is, their type. Storage space is allocated by variable type at compile time. The pointer variable must be defined as a pointer type. Let's look at one specific example:
int I, J; Define integer variable i,j
int *pointer_1, *pointer_2; Define pointer variable *pointer_1,*pointer_2
The int at the beginning of line 2nd means that the defined pointer variable is a pointer variable that points to an integral type of data. That is, pointer variables pointer_1 and pointer_2 can only be used to point to integer data (such as I and j) and not to floating-point variables A and B. This int is the base type of the pointer variable. The base type of a pointer variable is used to specify the type of variable that the pointer variable can point to.
The general form of defining pointer variables is:
Base type * pointer variable name;
Here are the legal definitions:
float *pointer_3; Pointer_3 is a pointer variable char *pointer_4 that points to single-precision data
;//Pointer_4 is a pointer variable that points to character data
Note that the pointer variable name is pointer_3 and pointer_4, not *pointer_3 and *pointer_4, that "*" is not part of the pointer variable name, and a "*" before the variable name when defining the variable is the pointer variable.
So how do you make a pointer variable point to another variable? Just assign the address of the variable you are pointing to to the pointer variable. For example:
pointer_1=&i; The address of the variable i is stored in the pointer variable pointer_1
pointer_2=&j;//The address of the variable j is stored in the pointer variable pointer_2
In this way, pointer_1 points to the variable i,pointer_2 and points to the variable J. See figure.
The general C + + compilation system allocates 4 bytes of storage units for each pointer variable to hold the address of the variable.
When defining pointer variables, be aware that you cannot assign an initial value to a pointer variable with an integer, and you must specify the base type when you define the pointer variable.
referencing pointer variables
There are two operators related to pointer variables:
& Take the address operator.
* pointer operator (or indirect access operator).
For example: &a is the address of variable A and *p is the storage unit pointed to by the pointer variable p.
"Example" accesses an integer variable through a pointer variable.
#include <iostream>
using namespace std;
int main ()
{
int a,b;//define integer variable a,b
int *pointer_1,*pointer_2;//define pointer variable *pointer_1,*pointer_2
a=100;b= 10; Assign a value to the a,b/
/pointer_1=&a; The address of variable A to pointer_1
pointer_2=&b;//Assign the address of variable A to pointer_2
cout<<a << "" <<b<<endl; Outputs A and B values
cout<<*pointer_1<< "" <<*pointer_2<<endl; Output *pointer_1 and *pointer_2 values return
0;
}
The results of the operation are:
(Value of A and B) (value of *pointer_1 and *pointer_2)
Please compare it with the graph analysis.
The "&" and "*" operators are explained below:
1 if the "pointer_1=&a;" has been executed Statement, what is the meaning of &*pointer_1? The "&" and "*" two operators have the same precedence, but they are combined from right to left, so the *pointer_1 operation is the variable a, and then the & operation is performed. Therefore, the &*pointer_1 is the same as the &a, which is the address of variable A.
If there is a pointer_2=&*pointer_1; its role is to assign &a (A's address) to pointer_2, if pointer_2 to point B, after the reset it no longer points to B, but also points to a.
2 What is the meaning of *&a? First the &a operation, the address of a, and then the * operation, that is, the &a point of the variable, *&a and *pointer_1 are the same (assuming that the "pointer_1=&a;"), they are equivalent to variable a. That is, *&a and a are equivalent, see figure.
"Example" enters A and b two integers, and outputs A and B (processed with pointer variables) in the order of first large and then small.
The idea to solve this problem is to set two pointer variables P1 and P2 to point to A and B respectively. So that the P1 point to the large in A and B, P2 point to the small, sequential output *P1,*P2 to achieve the first large, small order output A and B. Follow this idea to write a program as follows:
#include <iostream>
using namespace std;
int main ()
{
int *p1,*p2,*p,a,b;
cin>>a>>b; Enter two integer
p1=&a;//P1 to point a
p2=&b;//Make P2 point to B if
(A<B)//If A<b Exchange P1 with P2 value
{
p=p1;p1= P2;p2=p; P2 the point of P1 to the Exchange
}
cout<< "a=" <<a<< "b=" <<b<<endl;
cout<< "max=" <<*p1<< "min=" <<*p2<<endl;
return 0;
}
The operating conditions are as follows:
4578↙
a=45 b=78
max=78 min=45
Enter the value of a value of 45,b 78, due to a<b, the value of P1 and P2 Exchange, the point of the P1 and P2 to the point of exchange.
Note that the algorithm for this problem is not to exchange the value of an integer variable, but rather to exchange the value of two pointer variables.