for array int a[10];
A represents the address of the first element of the array, that is, the &a[0];
If you make the pointer p, point to the first element of the array, you can do so:
int * P=A;
Or
int *p=&a[0];
So p++, is to point to the first element in the array, that is a[1];
At this point *p is the value that is placed in a[1].
At this point, a[i]=p[i]=* (a+i) =* (p+i)
Let me give you an example;
Directly using A[i] to output
Copy Code code as follows:
#include <iostream>
using namespace Std;
int main () {
int a[10]={1,2,3,4,5,6,7,8,9,10};
cout<< "Please input intergers:" <<endl;
int i=0;
for (i=0;i<10;i++)
cout<<a[i]<< "";
cout<<endl;
return 0;
}
use * (a+i) to output
Copy Code code as follows:
#include <iostream>
using namespace Std;
int main () {
int a[10]={1,2,3,4,5,6,7,8,9,10};
cout<< "Please input intergers:" <<endl;
int i=0;
for (i=0;i<10;i++)
cout<<* (a+i) << "";
cout<<endl;
return 0;
}
use * (p+i) to output
Copy Code code as follows:
#include <iostream>
using namespace Std;
int main () {
int a[10]={1,2,3,4,5,6,7,8,9,10};
cout<< "Please input 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, the binding direction is from right and left, so it is equivalent to * (p++). The effect is to get the value of the variable that p points to (that is, *p), and then add the value of P to 1.
Copy Code code as follows:
#include <iostream>
using namespace Std;
int main () {
int a[10]={1,2,3,4,5,6,7,8,9,10};
cout<< "Please input intergers:" <<endl;
int i=0;
int * P=A;
while (P<A+10) {
cout<<*p++<< "T";
}
cout<<endl;
return 0;
}
equivalent to
Copy Code code as follows:
#include <iostream>
using namespace Std;
int main () {
int a[10]={1,2,3,4,5,6,7,8,9,10};
cout<< "Please input 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++), and * (++p) means first p+1, then *p.
Copy Code code as follows:
#include <iostream>
using namespace Std;
int main () {
int a[10]={1,2,3,4,5,6,7,8,9,10};
cout<< "Please input intergers:" <<endl;
int i=0;
int * P=A;
while (P<A+10) {
cout<<* (++p) << "T";
}
cout<<endl;
return 0;
}
Run the above program, the result will output the value of a[2] to a[11], where a[11] is not defined.