在打印函数 foo1 中“X”的地址和值以及 foo2 中 Y 的地址和值时,为什么这两个函数显示相同的值?
#include <stdio.h
void foo1(int xval)
{
int x;
x = xval;
/* print the address and value of x here */
}
void foo2(int dummy)
{
int y;
/* print the address and value of y here */
}
int main()
{
foo1(7);
foo2(11);
return 0;
}
程序的输出是
Address of X is: 65518
X 的值为:7
Address of Y is: 65518
Y 的值为:7
最佳答案
这是因为它们是在堆栈上创建的,每次函数调用后都会将其展开。因此它们是在相同的内存地址创建的。
关于c++ - c中不同函数的不同变量的相同地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24117002/