c++ - c中不同函数的不同变量的相同地址

标签 c++ c variables pointers memory-address

在打印函数 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/

相关文章:

bash - 从 awk 在当前 shell 中设置变量

swift - 用于创建类实例或创建空变量的 If/Switch 语句

c++单例类实例访问整个应用程序

c++ - strftime 性能与 snprintf

c - popen 与 C 中的系统函数

c - 保存部分结构

c++ - 动态内存重新分配

c++ - 在压缩和未压缩数据之间切换

c - '&' 操作后存储的值是多少

sql - 如何让变量在 Oracle SQL Developer 中真正起作用?