| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 641 人关注过本帖
标题:[求助]返回局部变量地址
取消只看楼主 加入收藏
cauwzy
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2006-4-12
收藏
 问题点数:0 回复次数:0 
[求助]返回局部变量地址

#include <iostream.h>
int& addressX(int x);//int& addressX(int& x);
int main()
{
int i=10;
int j = 20;
int *y;
int *x;
y = new int;
x= new int;

cout<<&i<<' '<<&j<<endl;
cout<<addressX(i)<<' '<<addressX(j)<<endl;
cout<<&addressX(i)<<' '<<&addressX(j)<<endl;//内存地址为给返回值分配的内存
y = &addressX(i);
x = &addressX(j);
*y = 15;
*x = 10000;
cout<<y<<' '<<x<<endl;
cout<<*y<<' '<<*x<<endl;
return 0;
}

int& addressX(int x) //int& addressX(int& x)
{
return x;
}

为什么cout<<y<<' '<<x<<endl;的输出是0x0013FF18 0x0013FF18 (x和y指向的地址相同?)

为甚么cout<<*y<<' '<<*x<<endl;这句的输出会为 4194820 4194820,为甚么不是15 10000

为什么当int& addressX(int x);换为int& addressX(int& x); 时就一切正常了

[此贴子已经被作者于2006-4-29 19:25:45编辑过]

搜索更多相关主题的帖子: 变量 局部 地址 
2006-04-29 19:21
快速回复:[求助]返回局部变量地址
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.014438 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved