For the array int a [10];
A Indicates the address of the first element of the array, that is, & a [0];
If the pointer p points to the first element of the array, you can perform the following operations:
Int * p =;
Or
Int * p = & a [0];
Then p ++ points to the first element in the array, that is, a [1];
* P is the value in a [1.
In this case, a [I] = p [I] = * (a + I) = * (p + I)
The following is an example;
Output directly using a [I]
#include<iostream> using namespace std; int main(){ int a[10]={1,2,3,4,5,6,7,8,9,10}; cout<<"Please input 10 intergers: "<<endl; int i=0; for(i=0;i<10;i++) cout<<a[i]<<" "; cout<<endl; return 0; } #include<iostream>using namespace std;int main(){ int a[10]={1,2,3,4,5,6,7,8,9,10}; cout<<"Please input 10 intergers: "<<endl; int i=0; for(i=0;i<10;i++) cout<<a[i]<<" "; cout<<endl; return 0;}
Output with * (a + I)
#include<iostream> using namespace std; int main(){ int a[10]={1,2,3,4,5,6,7,8,9,10}; cout<<"Please input 10 intergers: "<<endl; int i=0; for(i=0;i<10;i++) cout<<*(a+i)<<" "; cout<<endl; return 0; } #include<iostream>using namespace std;int main(){ int a[10]={1,2,3,4,5,6,7,8,9,10}; cout<<"Please input 10 intergers: "<<endl; int i=0; for(i=0;i<10;i++) cout<<*(a+i)<<" "; cout<<endl; return 0;}
Output with * (p + I)
#include<iostream> using namespace std; int main(){ int a[10]={1,2,3,4,5,6,7,8,9,10}; cout<<"Please input 10 intergers: "<<endl; int i=0; int * p=a; for(i=0;i<10;i++) cout<<*(p+i)<<" "; cout<<endl; return 0; } #include<iostream>using namespace std;int main(){ int a[10]={1,2,3,4,5,6,7,8,9,10}; cout<<"Please input 10 intergers: "<<endl; int i=0; int * p=a; for(i=0;i<10;i++) cout<<*(p+i)<<" "; cout<<endl; return 0;}
About * p ++
Because ++ and * have the same priority and the combination direction is from right to left, it is equivalent to * (p ++ ). The function is to first obtain the value of the variable pointed to by p (* p), and then add 1 to the value pointed to by p.
[
#include<iostream> using namespace std; int main(){ int a[10]={1,2,3,4,5,6,7,8,9,10}; cout<<"Please input 10 intergers: "<<endl; int i=0; int * p=a; while(p<a+10){ cout<<*p++<<"\t"; } cout<<endl; return 0; } #include<iostream>using namespace std;int main(){ int a[10]={1,2,3,4,5,6,7,8,9,10}; cout<<"Please input 10 intergers: "<<endl; int i=0; int * p=a; while(p<a+10){ cout<<*p++<<"\t"; } cout<<endl; return 0;}
Equivalent
#include<iostream> using namespace std; int main(){ int a[10]={1,2,3,4,5,6,7,8,9,10}; cout<<"Please input 10 intergers: "<<endl; int i=0; int * p=a; while(p<a+10){ cout<<*p<<"\t"; p++; } cout<<endl; return 0; } #include<iostream>using namespace std;int main(){ int a[10]={1,2,3,4,5,6,7,8,9,10}; cout<<"Please input 10 intergers: "<<endl; int i=0; int * p=a; while(p<a+10){ cout<<*p<<"\t"; p++; } cout<<endl; return 0;}
* P ++ is equivalent to * (p ++). * (++ p) indicates that p + 1 is used first, and * p is used.
#include<iostream> using namespace std; int main(){ int a[10]={1,2,3,4,5,6,7,8,9,10}; cout<<"Please input 10 intergers: "<<endl; int i=0; int * p=a; while(p<a+10){ cout<<*(++p)<<"\t"; } cout<<endl; return 0; } #include<iostream>using namespace std;int main(){ int a[10]={1,2,3,4,5,6,7,8,9,10}; cout<<"Please input 10 intergers: "<<endl; int i=0; int * p=a; while(p<a+10){ cout<<*(++p)<<"\t"; } cout<<endl; return 0;}
Running the above program will output the values from a [2] to a [11], where a [11] is not defined.