c指针的各种用法试验

来源:互联网 发布:什么网络加速器最好 编辑:程序博客网 时间:2024/05/18 02:56

  1. #include <iostream.h>  
  2. void fun0(int* p)  
  3. {  
  4.     int* a=new int[3];  
  5.     *a=0;  
  6.     *(a+1)=1;  
  7.     *(a+2)=2;  
  8.     p=a;  
  9. }  
  10.   
  11. void fun1(int* &p)  
  12. {  
  13.     int* a=new int[3];  
  14.     *a=0;  
  15.     *(a+1)=1;  
  16.     *(a+2)=2;  
  17.     p=a;  
  18. }  
  19.   
  20. void fun2(int* p)  
  21. {  
  22.     *p=0;  
  23.     *(p+1)=1;  
  24.     *(p+2)=2;  
  25. }  
  26.   
  27. //warning:returning address of local variable or temporary  
  28. int* fun3()  
  29. {  
  30.     int a[]={1,2,3};  
  31.     return a;  
  32. }  
  33.   
  34. //warning C4172: returning address of local variable or temporary  
  35. int*& fun4()  
  36. {  
  37.     int* a=new int[3];  
  38.     a[0]=0;  
  39.     a[1]=1;  
  40.     a[2]=2;   
  41.     return a;  
  42. }  
  43.   
  44. void fun5(int* p[3])  
  45. {  
  46.     *p[0]=0;    *(p[0]+1)=10;    
  47.     *p[1]=1;    *(p[1]+1)=11;  
  48.     *p[2]=2;    *(p[2]+1)=12;  
  49. }  
  50.   
  51. //cannot convert from 'int [3]' to 'int *&  
  52. /* 
  53. int*& fun6() 
  54. { 
  55.     int a[]={1,2,3}; 
  56.     return a; 
  57. } 
  58. */  
  59.   
  60. void main()  
  61. {  
  62.     int* p=new int[3];  
  63. //  int* p;     //runtime error:the memory connot be written  
  64.                 //it is necessory to allocate memory  
  65.     cout<<"test:  void fun0(int* p)"<<endl;  
  66.     fun0(p);  
  67.     for(int i=0;i<3;i++){  
  68.         cout<<*(p+i)<<endl;  
  69.     }  
  70.     delete[3] p;  
  71.   
  72.     p=new int[3];  
  73.     cout<<endl<<"test:  void fun1(int* &p)"<<endl;  
  74.     fun1(p);  
  75.     for(i=0;i<3;i++){  
  76.         cout<<*(p+i)<<endl;  
  77.     }  
  78.     delete[3] p;  
  79.   
  80.     p=new int[3];  
  81.     cout<<endl<<"test:  void fun2(int* p)"<<endl;  
  82.     cout<<"    allocate memory for the parameter"<<endl;  
  83.     fun2(p);  
  84.     for(i=0;i<3;i++){  
  85.         cout<<"    "<<*(p+i)<<endl;  
  86.     }  
  87.     cout<<"    donot allocate memory for the parameter"<<endl;  
  88.     cout<<"    memory connot be written"<<endl;  
  89.     /*  
  90.     int* pt; 
  91.     fun2(pt);   //runtime error:memory connot be written 
  92.     for(i=0;i<3;i++){ 
  93.         cout<<*(pt+i)<<endl; 
  94.     } 
  95.     */  
  96.     delete[3] p;  
  97.   
  98.     p=new int[3];     
  99.     cout<<endl<<"test:  int* fun3()"<<endl;  
  100.     cout<<"    debug assertion failed"<<endl;  
  101.     /* 
  102.     p=fun3();   //debug assertion failed 
  103.     for(i=0;i<3;i++){ 
  104.         cout<<*(p+i)<<endl; 
  105.     } 
  106.     */  
  107.     delete[3] p;  
  108.   
  109.     p=new int[3];     
  110.     cout<<endl<<"test:  int*& fun4()"<<endl;  
  111.     p=fun4();  
  112.     for(i=0;i<3;i++){  
  113.         cout<<*(p+i)<<endl;  
  114.     }  
  115.   
  116. //  delete[3] p;//??? Debug assertion failed  
  117.   
  118.     cout<<endl<<"test:  void fun5(int* p[3])"<<endl;  
  119.     int* p2[3];  
  120.     //it's necessory to allocate memory  
  121.     for(i=0;i<3;i++){  
  122.         p2[i]=new int[2];  
  123.     }  
  124.     fun5(p2);  
  125.     for(i=0;i<3;i++){  
  126.         cout<<*p2[i]<<"   "<<*(p2[i]+1)<<endl;  
  127.     }  
  128.   
  129.     delete[6] *p2;  //also right in this form  
  130. //  delete[2] p2[0];  
  131. //  delete[2] p2[1];  
  132. //  delete[2] p2[2];  
  133. }  

实验结果

0 0
原创粉丝点击