*与*&的问题。
void ptr(int* &t)
{
if(t==NULL)
{
t=new int;
*t=3;
cout<<*t<<endl;
}
else
{
*t=4;
cout<<*t<<endl;
}
}
int *t=NULL; //*t为空 这时候必须为(int* &t)才能修改原来*t的值。
cout<<*t<<endl;
ptr(t);
cout<<*t<<endl;
return 0;
//////////////////////////////////////////////////////
void ptr(int* t)
{
if(t==NULL)
{
t=new int;
*t=3;
cout<<*t<<endl;
}
else
{
*t=4;
cout<<*t<<endl;
}
}
int a=0;//*t不为空。
int*t=&a; //这时候可以不加&。
cout<<*t<<endl;
ptr(t);
cout<<*t<<endl;
return 0;
差别在*t为空或不为空,参数加不加&的问题。为什么?