【问题标题】:What differences are there between addresses using & operator and without & operator? [duplicate]使用 & 运算符和不使用 & 运算符的地址有什么区别? [复制]
【发布时间】:2019-12-27 01:50:44
【问题描述】:

我遇到了一个指针变量的两个不同地址。我不知道它们是什么意思。为什么两个输出有两个不同的地址?

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"

【问题讨论】:

  • 第一个是指针持有的地址。第二个是指针本身的地址。

标签: c pointers


【解决方案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 );

【讨论】:

    猜你喜欢
    • 2016-04-28
    • 2011-01-02
    • 2011-06-30
    • 1970-01-01
    • 2022-06-10
    • 1970-01-01
    • 2012-12-19
    • 2016-06-22
    • 1970-01-01
    相关资源
    最近更新 更多