C - 指针和指针位置之间的差异

  • 本文关键字:指针 之间 位置 c pointers
  • 更新时间 :
  • 英文 :


此代码为 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

最新更新