#include <iostream.h> void fun0(int* p) { int* a=new int[3]; *a=0; *(a+1)=1; *(a+2)=2; p=a; } void fun1(int* &p) { int* a=new int[3]; *a=0; *(a+1)=1; *(a+2)=2; p=a; } void fun2(int* p) { *p=0; *(p+1)=1; *(p+2)=2; } //warning:returning address of local variable or temporary int* fun3() { int a[]={1,2,3}; return a; } //warning C4172: returning address of local variable or temporary int*& fun4() { int* a=new int[3]; a[0]=0; a[1]=1; a[2]=2; return a; } void fun5(int* p[3]) { *p[0]=0; *(p[0]+1)=10; *p[1]=1; *(p[1]+1)=11; *p[2]=2; *(p[2]+1)=12; } //cannot convert from 'int [3]' to 'int *& /* int*& fun6() { int a[]={1,2,3}; return a; } */ void main() { int* p=new int[3]; // int* p; //runtime error:the memory connot be written //it is necessory to allocate memory cout<<"test: void fun0(int* p)"<<endl; fun0(p); for(int i=0;i<3;i++){ cout<<*(p+i)<<endl; } delete[3] p; p=new int[3]; cout<<endl<<"test: void fun1(int* &p)"<<endl; fun1(p); for(i=0;i<3;i++){ cout<<*(p+i)<<endl; } delete[3] p; p=new int[3]; cout<<endl<<"test: void fun2(int* p)"<<endl; cout<<" allocate memory for the parameter"<<endl; fun2(p); for(i=0;i<3;i++){ cout<<" "<<*(p+i)<<endl; } cout<<" donot allocate memory for the parameter"<<endl; cout<<" memory connot be written"<<endl; /* int* pt; fun2(pt); //runtime error:memory connot be written for(i=0;i<3;i++){ cout<<*(pt+i)<<endl; } */ delete[3] p; p=new int[3]; cout<<endl<<"test: int* fun3()"<<endl; cout<<" debug assertion failed"<<endl; /* p=fun3(); //debug assertion failed for(i=0;i<3;i++){ cout<<*(p+i)<<endl; } */ delete[3] p; p=new int[3]; cout<<endl<<"test: int*& fun4()"<<endl; p=fun4(); for(i=0;i<3;i++){ cout<<*(p+i)<<endl; } // delete[3] p;//??? Debug assertion failed cout<<endl<<"test: void fun5(int* p[3])"<<endl; int* p2[3]; //it's necessory to allocate memory for(i=0;i<3;i++){ p2[i]=new int[2]; } fun5(p2); for(i=0;i<3;i++){ cout<<*p2[i]<<" "<<*(p2[i]+1)<<endl; } delete[6] *p2; //also right in this form // delete[2] p2[0]; // delete[2] p2[1]; // delete[2] p2[2]; }