双11狂欢大促

盲盒赢 定制羽绒服

开启惊喜盲盒,抽取你的专属好礼

— 惊喜奖品抢先看 —

*与*&的问题。

renzha0013 renzha0013 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
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为空或不为空,参数加不加&的问题。为什么?
...全文
评论 7
收藏 已收藏
分享
返回顶部
renzha0013
C++ 语言 65,214 成员 250,514 内容