使用&运算符和不使用&运算符的地址之间有什么区别?

时间:2019-08-21 18:34:58

标签: c pointers

我遇到了两个不同的指针变量地址,我不知道它们的含义。为什么两个输出有两个不同的地址?

char *name = "John";
printf("is stored at %p\n",name ); //output that is showed "is stored at 0x558b8c21e9c4"
printf("print on the screen %p\n",&name);//output that is showed "print on the screen 0x7ffd8b9be710"

1 个答案:

答案 0 :(得分:1)

变量name包含字符串文字"John"的地址。

所以这个电话

printf("is stored at %p\n",name );

输出字符串文字的第一个字符的地址。

表达式&name包含变量name的地址,并且具有类型char **而不是类型char *,因此printf的第二次调用输出变量name本身的地址,而不是字符串文字的地址。

您应该将输出的指针强制转换为类型void *。例如

printf("is stored at %p\n", ( void * )name );