我遇到了两个不同的指针变量地址,我不知道它们的含义。为什么两个输出有两个不同的地址?
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"
答案 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 );