[求助]返回局部变量地址
#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编辑过]