#include <iostream>
using namespace Std;
int main ()
{
Double * P3=new double[3];
p3[0]=0.2;
p3[1]=0.5;
p3[2]=0.8;
cout<< "P3[1" is "<<p3[1]<<endl;
p3=p3+1;
cout<< "now P3[0) is" <<p3[0]<<endl;
cout<< "P3[1" is "<<p3[1]<<endl;
P3=p3-1;
delete [] P3;
return 0;
}
The following is the output of the program:
P3[1] is 0.5
Now P3[0] was 0.5 and p3[1] is 0.8
As you can see, the pointer can be used as the array name, P3[0] as the first element, and so on. The following line of code indicates the difference between the array name and the pointer.
p3=p3+1;
The value of the array name cannot be modified. But the pointer is a variable, so you can modify his value. Note the effect of adding P3 to 1. Expression P3[0] Now refers to the second value of the array. Therefore, adding P3 to 1 causes it to point to the 2nd element instead of the 1th element. After you subtract 1, the pointer points to the original value so that the program can provide the correct address for delete[.
Yemi qq:877451122
Add me to the discussion of C + + technology OH!!!!!
How to use dynamic arrays