这段代码为什么不能实现我所想的效果?

gaoxun2010 2010-09-24 04:10:12
#include <iostream>
using namespace std;
void funcTest1(int&,int*);
void funcTest2(int&,int*);
int main()
{
int endLine;
int first,second;
int *pntSecond=&second;
cout<<"Please input two number:";
cin>>first>>second;
cout<<"first="<<first<<",second="<<second<<endl;
funcTest1(first,pntSecond);
cout<<"first="<<first<<",second="<<second<<",*pntSecond="<<*pntSecond<<endl;
funcTest2(first,pntSecond);
cout<<"first="<<first<<",second="<<second<<",*pntSecond="<<*pntSecond<<endl;
cin>>endLine;
return 0;
}
void funcTest1(int &first,int *second)
{
first=99;
*second=999;
}
void funcTest2(int &first,int *second)
{
second=&first;
}

为什么funcTest2()执行后,*pntSecond依然指向999,而不是first的99呢?
我的运行环境:mac+xcode
...全文
74 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
gaoxun2010 2010-09-24
  • 打赏
  • 举报
回复
谢谢,明白了,我以为指针本身就是有引用特性呢。
pengzhixi 2010-09-24
  • 打赏
  • 举报
回复
你修改的是临时指针的指向
pengzhixi 2010-09-24
  • 打赏
  • 举报
回复
void funcTest2(int &first,int *&second)
justkk 2010-09-24
  • 打赏
  • 举报
回复
void funcTest2(int &first,int *second)
这种方式是修改不了second对应的实参的

可以考虑指针的指针,或指针的引用

64,648

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧