1 int main() 2 { 3 int updates=6; //定义int变量updates 4 int * p_updates; //定义指针p_updates 5 p_updates=&updates;//将updates的地址赋给指针p_upfates 6 7 cout<<"value:update="<<updates<<endl; //updates=6 8 cout<<"*p_updates="<<*p_updates<<endl; //*p_updates为地址中存储的值 即6 9 10 cout<<"address:&updates="<<&updates<<endl; //&update 取地址 11 cout<<"p_updates="<<p_updates<<endl; //p_updates 地址值 12 13 *p_updates=*p_updates+1; //地址中存储的值加1 即6+1=7 14 cout<<"now updates="<<updates<<endl; //update=7 15 system("pause"); 16 return 0; 17 }
1 int main() 2 { 3 int higgens=5; 4 int *pt=&higgens; 5 6 cout<<"value of higgens="<<higgens<<endl; //5 7 cout<<"address of higgens="<<&higgens<<endl; //地址 8 cout<<"value of *pt="<<*pt<<endl; //5 9 cout<<"value of pt="<<pt<<endl; //地址 10 system("pause"); 11 }
1 int main() 2 { 3 double * p3=new double [3];//定义一个double类型的指针 4 p3[0]=0.2; 5 p3[1]=0.5; 6 p3[2]=0.8; 7 cout<<"p3[1] is"<<p3[1]<<endl; //p3当做数组名使用,p3[1]为第二个元素 8 p3=p3+1;//修改指针的值,+1之后p3[0]指向的是之前的第二个值 9 cout<<"now p3[0] is"<<p3[0]<<endl; 10 cout<<"p3[1] is"<<p3[1]<<endl; 11 p3=p3-1; //为delete []提供正确的地址 12 delete [] p3; 13 14 system("pause"); 15 return 0; 16 }
1 int main() 2 { 3 double wages[3]={10000.0,20000.0,30000.0}; 4 short stacks[3]={3,2,1}; 5 double *pw=wages; 6 short *ps=&stacks[0]; 7 8 cout<<"pw="<<pw<<",*pw="<<*pw<<endl; 9 pw=pw+1; 10 cout<<"pw="<<pw<<",*pw="<<*pw<<endl; 11 cout<<"ps="<<ps<<",*ps="<<*ps<<endl; 12 ps=ps+1; 13 cout<<"ps="<<ps<<",*ps="<<*ps<<endl; 14 cout<<"stacks[0]="<<stacks[0]<<",stacks[1]="<<stacks[1]<<endl; 15 cout<<"*stacks="<<*stacks<<",*(stacks+1)="<<*(stacks+1)<<endl; 16 17 cout<<sizeof(wages)<<"=size of wages array"<<endl;//数组长度 3*8=24 18 cout<<sizeof(pw)<<"=size of pw pointer"<<endl; //指针长度 4 在32位机上,所有指针类型变量占用内存字节数都为4 ,64位机指针占用的内存大小为8个字节 19 system("pause"); 20 return 0; 21 }