此代码为 i 和 &i 打印不同的值,并且它们都不等于 10。请解释一下这两个数字的含义。
#include<stdio.h>
int main(){
int p=10;
int *i=&p;
printf("%d %d",i,&i);
}
这是输出的样子
i
是一个integer pointer
,将用于存储integer variable
的地址。在这种情况下,i
存储在主存储器的堆栈区域中,当您打印&i
时,这意味着您打印存储i
的位置的地址。当您打印i
时,这意味着您打印的值为i
(i
的值是p
的地址,因为您已通过此行int *i=&p;
将&p
分配给i
(。我希望它对你有用。
这是代码的修改版本,在printfs中带有注释。 请注意,我添加了第三个 printf 来引用您在p
中的int
#include<stdio.h>
int main(){
int p=10;
int *i=&p;
printf("'i' = %p is the address of the int stored in variable pn",(void *)i);
printf("'&i' = %p is the address of the pointer to an int called in",(void *)&i);
printf("'*i' = %d is the int that is stored at the location in i which points to pn",*i);
}
'i' = 0x7ffee4e63abc is the address of the int stored in variable p
'&i' = 0x7ffee4e63ab0 is the address of the pointer to an int called i
'*i' = 10 is the int that is stored at the location in i which points to p