C 指向作为值传递的参数的指针?
在以下示例中:C 指向作为值传递的参数的指针?,c,pointers,scope,arguments,C,Pointers,Scope,Arguments,在以下示例中: #include <stdio.h> #include <stdlib.h> void test1 (int x) { int * xp = &x; printf("Address of xp (test1): [%p]\n", &xp); printf("xp points to: [%p] which is the same as [%p]\n", xp, &x); } int main() {
#include <stdio.h>
#include <stdlib.h>
void test1 (int x)
{
int * xp = &x;
printf("Address of xp (test1): [%p]\n", &xp);
printf("xp points to: [%p] which is the same as [%p]\n", xp, &x);
}
int main()
{
int x = 4;
printf("Address of x (main): [%p] \n", &x);
test1(x);
return 0;
}
- 地址
(中参数的地址)会发生什么变化 测试1)在函数之外李>0xbfca0c40
- 它在内存中的位置是什么
很可能是在机器堆栈上,因为堆栈是实现参数传递和局部变量的非常常见的方式。当退出
test1()
函数时,堆栈空间将被释放,因此相关内存可以重新使用。int xtest1
正式参数列表中的code>是test1
的局部变量。它的状态类似于如果你也去了inty代码>作为test1
中的声明。它具有自动存储时间
退出test1
后,变量不再存在
通常,编译器通过包含堆栈结构的内存块实现自动存储。调用函数时,会在堆栈顶部为该函数的自动变量(以及返回地址和任何需要保存的寄存器)声明新空间;当您离开函数时,堆栈会“弹出”到调用函数之前的位置
地址仍将包含x的值,直到某些程序将重用它
地址在堆栈上,因为变量x是函数参数
我真的不明白。显然xp
和&x
具有相同的值。毕竟你写了xp=&x
。我不明白第一个要点,至于第二个,它在哪里,呃,什么在哪里?
Address of x (main): [0xbfca0c5c]
Address of xp (test1): [0xbfca0c2c]
xp points to: [0xbfca0c40] which is the same as [0xbfca0c40]