Summary of pointer data types and pointer arithmetic

Some pointer operations have been used before (such as p ++, p + i, etc.), and now all pointer operations are listed below.

1) Pointer variable adds/subtracts an integer

For example:p ++, p--, p + i, p-i, p + -i, p-=i, etc.

C++ stipulates that adding or subtracting an integer to a pointer variable is to add or subtract the original value of the pointer variable (which is an address) and the number of bytes of the memory unit occupied by the variable it points to.For example, p + i represents such an address calculation:p + i * d, where d is the number of bytes occupied by the variable unit pointed to by p.This will ensure that p + i points to the i-th element below p.

2) pointer variable assignment

Assign a variable address to a pointer variable.Such as:

p =&a;//Assign the address of variable a to p
 p=array;//Assign the address of the first element of the array to p
 p =&array [i];//Assign the address of the i-th element of array to p
 p=max;//max is a defined function,Assign the entry address of max to p
 p1=p2;//p1 and p2 are pointer variables of the same type,Assign the value of p2 to p1

3) pointer variables can have null values,That the pointer variable does not point to any variable,Can be expressed as:


In fact, null represents the integer 0, which makes p point to the unit with address 0. This keeps the pointer from pointing to any valid unit.Actually the system has defined null first:

#define null 0

The above null definition is included in the iostream header file, and null is a symbolic constant.It should be noted that the value of p is equal to null and p is not assigned are two different concepts.

Any pointer variable or address can be compared with null for equality or inequality,Such as:

if (p == null) p=p1;

4) Two pointer variables can be subtracted

If two pointer variables point to elements of the same array,The difference between the values ​​of the two pointer variables is the number of elements between the two pointers,See Figure 6.25.

If p1 points to a [1] and p2 points to a [4], then p2-p1=(a + 4)-(a + 1)=4-1=3, but p1 + p2 has no practical meaning.

5) Comparison of two pointer variables

If two pointers point to elements of the same array,You can compare them.The pointer variable to the preceding element is smaller than the pointer variable to the following element.As shown in Figure 6.25, p1

6) Assignment to pointer variables should pay attention to type issues

The basic concepts and preliminary applications of pointers were introduced in the previous sections of this chapter.It should be stated,Pointers are an important concept in C and C++,Is a feature of c and c++.The advantages of using pointers are:

Increase program efficiency;

When calling a function,If you change the value of some variable in the called function,These values ​​can be used by the key function,That is, through the function call,Get multiple changeable values;

Can achieve dynamic storage allocation.

But at the same time you should see thatThe use of pointers is too flexible,For skilled programmers,You can use it to write distinctive and high-quality programs.Implement many features that are difficult to achieve in other high-level languages,But it's also very error prone,And such errors are often difficult to find.

C++ pointer array and pointer to pointer

Pointer array concept

If an array,Its elements are pointer-type data,This array is called a pointer array,That is,Each element in the pointer array is equivalent to a pointer variable,Its values ​​are all addresses.The definition of a one-dimensional pointer array is:

Type name * array name [array length];


int * p [4];

You can use the elements in the pointer array to point to several strings,Makes string processing more convenient and flexible.

[Example] Several character strings are output in alphabetical order (small to large).

using namespace std;
int main ()
 void sort (char * name [], int n);//declare function
 void print (char * name [], int n);//declare function
 char * name []={"basic", "fortran", "c++", "pascal", "cobol"};//define pointer array
 int n=5;
 sort (name, n);
 print (name, n);
 return 0;
void sort (char * name [], int n)
 char * temp;
 int i, j, k;
 for (i=0;i<n-1;i ++)
  for (j=i + 1;j<n;j ++)
   if (strcmp (name [k], name [j])>0) k=j;
  if (k!=i)
   temp=name [i];name [i]=name [k];name [k]=temp;
void print (char * name [], int n)
 int i;
 for (i=0;i<n;i ++)
  cout<name [i]<endl;

The result is:


The print function is used to output each string.name [0] ~ name [4] is the first address of each string.The print function can also be rewritten as:

void print (char * name [], int n)
 int i=0
 char * p;
 p=name [0];
 while (i<n)
  p=* (name + i ++);

"* (Name + i ++)" means to find the value of * (name + i) first, that is, name [i] (it is an address). Assign it to p, then i plus 1. Finally output a string starting with p address.

Pointer to pointer

After mastering the concept of pointer arrays,The following introduces pointers to pointer data.Referred to as a pointer to a pointer.As can be seen from Figure 6.22,name is an array of pointers,Each element of it is a pointer-type data (its value is an address), each pointing to a different string.The array name name represents the address of the first element of the pointer array.name + i is the address of name [i]. Since the value of name [i] is an address (that is, a pointer), name + i is a pointer to pointer-type data.You can also set a pointer variable p, which points to the elements of the pointer array (see figure). p is a pointer variable to pointer data.

How to define a pointer variable to pointer data?as follows:

char * (* p);

As can be seen from Appendix b,The combination of * operators is from right to left,So "char * (* p);" can be written as:

char ** p;

[Example] A pointer variable to character data.

using namespace std;
int main ()
 char ** p;//Define a pointer variable p to the character pointer data
 char * name []={"basic", "fortran", "c++", "pascal", "cobol"};
 p=name + 2;//see p in Figure 6.23
 cout<<* p<endl;//output the string pointed to by name [2]
 cout<<** p<endl;//print the first character in the string pointed to by name [2]

The result is:


The elements of the pointer array may not point to the string,And point to integer data or single-precision data.

The way to "indirect access" to a variable has already been mentioned at the beginning of this chapter.Using pointer variables to access another variable is "indirect access". If the address of a target variable is stored in a pointer variable,This is the "single-level intersite", see Figure 6.24 (a). The pointer to the pointer uses the "secondary indirect address" method. See below. In theory,The indirect method can be extended to more levels,See below. In practice, however, there are rarely more than two secondary sites in the program.

  • Previous Talking about the difference between CMD and win powershell
  • Next JavaScript face recognition technology and face recognition JavaScript class library Trackingjs