Home>

A pointer variable is a special variable,It differs from other types of variables I have learned before:it is used to point to another variable.To represent the connection between a pointer variable and the variable it points to,In C++, use the "*" symbol to indicate pointing,For example, i_pointer is a pointer variable,And * i_pointer represents the variable pointed to by i_pointer.

The following two statements have the same effect:

 i=3;
  * i_pointer=3;

Defining pointer variables

C++ stipulates that all variables must be defined before use,That is, specify its type.Storage space is allocated by variable type at compile time.It must be defined as a pointer type for pointer variables.First look at a specific example:

 int i, j;//Define integer variables i, j
  int * pointer_1, * pointer_2;//Define pointer variables * pointer_1, * pointer_2

The int at the beginning of line 2 means that the defined pointer variable is a pointer variable pointing to integer data.That is,Pointer variables pointer_1 and pointer_2 can only be used to point to integer data (such as i and j), but 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 a pointer variable is:

Base type * pointer variable name;

The following are legal definitions:

 float * pointer_3;//pointer_3 is a pointer variable to single-precision data
  char * pointer_4;//pointer_4 is a pointer variable to character data

Please note:pointer variable names are pointer_3 and pointer_4, not * pointer_3 and * pointer_4, that is, "*" is not part of the pointer variable name.When defining a variable, add an "*" before the variable name to indicate that the variable is a pointer variable.

So how do you make a pointer variable point to another variable?You only need to assign the address of the pointed variable to a pointer variable.E.g:

 pointer_1 =&i;//Store the address of the variable i in the pointer variable pointer_1
  pointer_2 =&j;//Store the address of the variable j in the pointer variable pointer_2

In this way, pointer_1 points to the variable i, and pointer_2 points to the variable j. See picture.

The general C++ compilation system allocates 4 bytes of storage units for each pointer variable.The address used to store the variable.

Pay attention to the definition of pointer variables:you cannot assign an initial value to a pointer variable with an integer,The base type must be specified when defining a pointer variable.

Reference pointer variable

There are two operators related to pointer variables:

&Take 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 pointer variable p.

[Example] Access integer variables through pointer variables.

#include<iostream>
using namespace std;
int main ()
{
  int a, b;//Define integer variables a, b
  int * pointer_1, * pointer_2;//Define pointer variables * pointer_1, * pointer_2
  a=100;b=10;//Assign a, b
  pointer_1 =&a;//Assign the address of variable a to pointer_1
  pointer_2 =&b;//Assign the address of variable a to pointer_2
  cout<a<""<b<endl;//output values ​​of a and b
  cout<<* pointer_1<<<<<<* pointer_2<<endl;//output values ​​of * pointer_1 and * pointer_2
  return 0;
}

The result is:

100 10 (values ​​of a and b)
100 10 (values ​​of * pointer_1 and * pointer_2)

Please analyze against the chart.

The "&" and "*" operators are explained below:

1) If the "pointer_1 =&a;" statement has been executed, what does&* pointer_1 mean?"&" And "*" have the same precedence,But combined from right to left,Therefore, the * pointer_1 operation is performed first, which is the variable a, and then the&operation is performed. Therefore,&* pointer_1 is the same as&a, which is the address of the variable a.

If there is pointer_2 =&* pointer_1;its role is to assign&a (the address of a) to pointer_2. If pointer_2 originally points to b, after reassignment, it no longer points to b, but also points to a.

2) What does *&a mean?First perform the operation of&a, get the address of a, and then perform the * operation, that is, the variable pointed to by&a,*&A and * pointer_1 have the same effect (assuming "pointer_1 =&a;" has been executed), they are equivalent to the variable a. That is, *&a is equivalent to a, as shown in the figure.

[Example] Enter two integers a and b,Output a and b in descending order (handled by pointer variables).

The idea to solve this problem is:set two pointer variables p1 and p2 so that they point to a and b, respectively. Point p1 to the larger of a and b,p2 points to the smaller one,Sequential output * p1, * p2 realizes outputting a and b in the order of big first and then small. Write the program according to this idea:

#include<iostream>
using namespace std;
int main ()
{
  int * p1, * p2, * p, a, b;
  cin>>a>>b;//Enter two integers
  p1 =&a;//make p1 point to a
  p2 =&b;//make p2 point to b
  if (a<b) //If a<b, swap the values ​​of p1 and p2
  {
   p=p1;p1=p2;p2=p;//Exchange the pointer of p1 with the pointer of p2
  }
  cout<"a =<<a<<"b="<<b<endl;
  cout<<"max ="<<* p1<"min ="<<* p2<endl;
  return 0;
}

The operation is as follows:

4578↙
a=45 b=78
max=78 min=45

Enter a value of 45 and a value of 78. Since a<b, swap the value of p1 and p2,That is, the pointer of p1 is exchanged with the pointer of p2.

Note that the algorithm for this problem is not to exchange the values ​​of integer variables,Instead, the values ​​of the two pointer variables are swapped.

c
  • Previous C # using Mutex to easily implement a single instance of the program
  • Next Explain the Stream concept in Java as an example