Data types and pointer operations based on pointer

  • 2020-04-02 01:25:08
  • OfStack

1. Pointer data type summary

The data type for the pointer
define meaning Int I; Define shaping variables Int * p; Defines the pointer variable p that only wants integer data Int a [n]. Define the array a, which has n elements Int * p [n]. Defines the pointer array p, which consists of n pointer elements pointing to the integer data type Int (* p) [n]. Defines a pointer to a one-bit array of n elements Int f (); F is the function that brings back the integer value Int * (p); P is a function that takes back a pointer to the integer data. Int (* p) (); P is a pointer to a function that returns an integer Int * * p; P is a pointer variable to a pointer, which points to a pointer variable for shaping data
2. Operation summary of pointer
(1) assignment of pointer variables


int a;
int *p
p=&a;

Assign the address of variable a to p

int a[3]={1,2,3};
int *p;
p=a;

Assigns the address of the first element of the array to pointer p

int a[3]={1,2,3};
int *p;
p=&a[2];

Assigns the address of the element in the array to the pointer p

int main(){
    int  f(int z);
    int (*p)(int z);
    p=f;
    p(5);
}
int f(int z ){
cout<<z<<endl;
}

F is a defined function that assigns the entry address of f to p

int a=3;
int *p1=&a;
int *p2=p1;

P1 and p2 are Pointers of the same type, assigning the value of p1 to p2

(2) null value of pointer variable
Pointer variable can have an empty value, that is, the pointer variable does not point to any variable, which can be expressed as follows:


p=NULL;

In fact, NULL represents the integer 0, meaning that p points to the cell whose address is 0. This prevents the pointer from pointing to any valid cell.
In fact, the system has already defined NULL:

#define NULL 0

The above definition of NULL is included in the iostream header in C++. NULL is a symbolic constant.
Run in the c-free editor:

#include<iostream>
using namespace std;
int main(){
 cout<<NULL;
 cout<<endl;
}


< img Alt = "" border = 0 SRC =" / / files.jb51.net/file_images/article/201309/2013090210065118.jpg ">

Note that the value of p equals NULL and p is not assigned. The former has a value (the value is 0) and does not point to any variable, while the latter does not assign a value to p but does not mean that p has no value, except that its value is an unexpected value, which means that p may point to some unspecified cell. This situation is very dangerous. Therefore, it is important to assign a variable before drinking it.

Any pointer variable or address can be compared with NULL equal or unequal:


if(p==NULL)p=p1;

The above statement can also be written:

if(!p)p=p1;

Also note
Int * p = NULL; And int * p; * p = NULL; The difference between:
Int *p=NULL is the definition of a pointer to the integer variable, and the pointer is initialized, the initial value is NULL;
Int * p; A pointer to an integer variable is defined, and because the pointer is not initialized, it may point to any value, and therefore to an invalid value, such as a variable in system memory.

Then * p = NULL; Is to set the value of the variable that p points to to 0, which is dangerous because the value that p points to is uncertain.

(3) the assignment of pointer variables should pay attention to the problem
We know that values can be assigned between different pointer variables of the same base type.
It is not possible to assign values between variables of different base types.
Running code:


#include<iostream>
using namespace std;
int main(){
<span style="white-space:pre"> </span>int *p1,i=5;
<span style="white-space:pre"> </span>double *p2 ,j=2.5;
<span style="white-space:pre"> </span>p1=&i;
<span style="white-space:pre"> </span>p2=&j;
 p1=p2;
    cout<<*p1<<endl;
    return 0;
}

Editor prompt:

< img Alt = "" border = 0 SRC =" / / files.jb51.net/file_images/article/201309/2013090210065219.jpg ">


The above assignment can be achieved by forcing type conversion:


#include<iostream>
using namespace std;
int main(){
 int *p1,i=5;
 double *p2 ,j=2.5;
 p1=&i;
 cout<<*p1<<endl;
 p2=&j;
 cout<<*p2<<endl;
 p1=(int *)p2;
    cout<<*p1<<endl;
    return 0;
}

Although the above operation no longer reports an error, the forced type translation of the pointer is truncated, so the desired result is still not obtained:

< img Alt = "" border = 0 SRC =" / / files.jb51.net/file_images/article/201309/2013090210065220.jpg ">


Casts on Pointers

(4) pointer variable plus/minus an integer

Such as:


p++;
p--;
p+i;
p-1;
p+=i;
p-=i;

C++ specifies that a pointer variable plus/minus an integer is the addition or subtraction of the original value of the pointer variable (the original address) and the number of bytes in memory occupied by the variable it points to.

Such as the p + I; Represents the address calculation: p+ I *d, d is the number of bytes occupied by the variable unit that p points to. So that means that p plus I points to the ith element below p.

(5) subtract two pointer variables
If two Pointers point to an element in the same array, the difference between two pointer variables is the number of elements between the two Pointers to that element.


#include<iostream>
using namespace std;
int main(){
 int a[10]={1,2,3,4,5,6,7,8,9,10};
 int *p1=&a[3];
 int *p2=&a[5];
 cout<<(p2-p1)<<endl;
 cout<<(p1-p2)<<endl;
 return 0;
}

Operation results:

< img Alt = "" border = 0 SRC =" / / files.jb51.net/file_images/article/201309/2013090210065221.jpg ">

(6) comparison of two pointer variables
If two Pointers point to an element of the same array, the size can be compared. The pointer variable to the preceding element is smaller than the pointer variable to the following element.

#include<iostream>
using namespace std;
int main(){
 int a[10]={1,2,3,4,5,6,7,8,9,10};
 int *p1=&a[3];
 int *p2=&a[5];
 if(p1<p2){
  cout<<"p1<p2"<<endl;
 }else{
  cout<<"p1>=p2"<<endl;
 }
 return 0;
}

Output:

< img Alt = "" border = 0 SRC =" / / files.jb51.net/file_images/article/201309/2013090210065222.jpg ">

You can also use this property to output all the elements in the array:


#include<iostream>
using namespace std;
int main(){
 int a[10]={1,2,3,4,5,6,7,8,9,10};
 int *p=a;
    while(p<a+10){
     cout<<*p<<endl;
     p++;
    }
 return 0;
}

Output results:

< img Alt = "" border = 0 SRC =" / / files.jb51.net/file_images/article/201309/2013090210065223.jpg ">


Related articles: