【发布时间】:2021-01-31 02:24:39
【问题描述】:
让我们看下面的例子,我们交换 a 和 b 的值。
void swap(int* a, int* b) {
// remember, a and be are both memory addresses now!
printf("The address of a is: %p\n", a);
printf("The address of address-of a is: %p\n", &a);
int tmp = *a;
*a=*b;
*b=tmp;
}
打印输出给我:
a的地址是:0x7ffd36e62d08
address-of a的地址为:0x7ffd36e62cd8
第二个printf 语句究竟告诉我们什么?例如:
int a = 4;
&a; // address-of a
int* b; = &a;
b; // address-of a
*b; // value-of a
&b; // address-of ...?
这只是堆栈上内存地址的位置吗?
【问题讨论】:
-
&b是指针变量b存储在堆栈中的地址