0
点赞
收藏
分享

微信扫一扫

C++ 指针篇01

//涉及的指针内容如下:
变量指针 
字符串指针 
数组指针
指针数组
const指针
指针的指针
指针函数

 
//变量指针 

 #include "stdafx.h" 

 #include <iostream> 

 #include "stdlib.h" 

   

 using namespace std; 


 int main(int argc,char* argv[]) 

 { 

      int a=5,b=6,*pa,*pb; 

      pa=&a; //指针变量pa指向变量a的地址 

      pb=&b; //指针变量pb指向变量b的地址 

      cout<<pa<<"->"<<*pa<<endl; 

      cout<<pb<<"->"<<*pb<<endl; 


 system("pause"); 

 return 0; 

 } 


 结果: 

 002FFB94->5 

 002FFB88->6 


 //字符串指针 

 #include "stdafx.h" 

 #include <iostream> 

 #include "stdlib.h" 

   

 using namespace std; 


 int main(int argc,char* argv[]) 

 { 

     char ss[30]={"i am a bird!"},*pss;  //字符串指针可以写成这样: char *parr2="i am a bird" 

     pss=ss;  //字符串的首地址赋给pss 

     //cout<<pss<<endl;  //输出整个字符串的内容 

     while(*pss != '\0')  //默认字符串的结束标识符\0 

     { 

     cout<<*pss; 

     pss++;       //指针地址顺序向后移动1个位置 

     } 

     cout<<endl; 


 system("pause"); 

 return 0; 

 } 



 执行结果: 

 i am a bird! 

 请按任意键继续. . . 



 //数组指针 //涉及二维三维数组 

数组指针:指向数组的指针 

指针数组(指针类型型数组):存放指针变量的数组 



 #include "stdafx.h" 

 #include <iostream> 

 #include "stdlib.h" 

   

 using namespace std; 


 int main(int argc,char* argv[]) 

 { 

     int arr2[2]={1,2}; 

     int  (*p)[2];    //数组指针 

     p=&arr2; 

     cout<<p<<endl; 

     cout<<(*p)[0]<<(*p)[1]<<endl; 


 system("pause"); 



 return 0; 

 } 



//指针数组 

 #include "stdafx.h" 

 #include <iostream> 

 #include "stdlib.h" 

   

 using namespace std; 


 int main(int argc,char* argv[]) 

 { 

     int arr2[2][2]={{1,2},{3,4}}; 

     int  *p[2];     //指针数组(指针类型的数组) 

     p[0]=arr2[0];   

     p[1]=arr2[1]; 

     cout<<*p[0]<<endl; 

     cout<<*p[1]<<endl; 


 system("pause"); 



 return 0; 

 } 


//指针数组 

 #include "stdafx.h" 

 #include <iostream> 

 #include "stdlib.h" 

   

 using namespace std; 


 int main(int argc,char* argv[]) 

 { 

     int arr2[2][2]={{1,2},{3,4}}; 

     int  *p[2];    //指针数组 

     p[0]=arr2[0];    

     p[1]=arr2[1]; 

     cout<<*(p[1]+1)<<endl;  //结果为4 

     cout<<*(p[0]+3)<<endl;  //结果为4 


 system("pause"); 



 return 0; 

 } 



//const指针,常量指针 

 //constant int * p 

 #include "stdafx.h" 

 #include <iostream> 

 #include "stdlib.h" 

   

 using namespace std; 


 int main(int argc,char* argv[]) 

 { 

     int a=3; 

     //const 

     const    int *p;  //定义为const 


     p=&a; 

     *p=5;    //编译的时候将报错,由于上边const原因 

     cout<<*p<<endl; 

     cout<<a<<endl; 


 system("pause"); 



 return 0; 

 } 



//const指针,指针常量 

 //int * const p 

 #include "stdafx.h" 

 #include <iostream> 

 #include "stdlib.h" 

 using namespace std; 


 int main(int argc,char* argv[]) 

 { 

     int a=3,b=9; 

     //const 

     int * const p=&a;  //定义为const  常量指针 


      p=&b;  //报错,p的指向内存地址值不能被修改 

     

     cout<<*p<<endl; 

     cout<<a<<endl; 


 system("pause"); 



 return 0; 

 } 



//指针的指针 

 #include "stdafx.h" 

 #include <iostream> 

 #include "stdlib.h" 

 using namespace std; 


 int main(int argc,char* argv[]) 

 { 

  int **p; 

     int arr1[2][2]={{1,2},{3,4}}; 

     int *arrp[2];  //定义指针数组,里边存储的是地址(指针) 

      

     arrp[0]=arr1[0];  //arrp[0]存储数组第一行的首地址 

     *arrp=arr1[0];   //*arrp等价于arrp[0] 

     p=arrp;        //arrp是存储数组变量的首地址,而变量又是地址,所以arrp就是指针中的指针可以赋给p变量 

      

     cout<<*arrp<<endl; 

     cout<<arrp[0]<<endl; 

     cout<<p<<endl; 

     cout<<arrp<<endl; 

     cout<<&arrp[0]<<endl; 


   system("pause"); 



 return 0; 

 } 


 //指针函数 

 #include "stdafx.h" 

 #include <iostream> 

 #include "stdlib.h" 

   

 using namespace std; 


 int *min_number(int *p1,int *p2)  //定义一个返回值为指针类型函数 

 { 

     if(*p1>*p2) 

   *p1=*p2;     //比较最小值 


     return p1;  //返回地址 

 } 



 int main(int argc,char* argv[]) 

 { 

  int a=21,b=11; 

  int *ee=min_number(&a,&b);  //返回的指针赋给一个指针变量 

  cout<<ee<<endl; 

  cout<<*ee<<endl; //输出地址指向的值 


     system("pause"); 



 return 0; 

 }

举报

相关推荐

0 条评论