小编bab*_*ain的帖子

为什么地址不一样?

为了理解指针,我编写了下面的代码。

#include <cstdio>
#include <iostream>
using namespace std;

int main() {
    string a = "hello";
    const char *st = &a[0];
    printf("%p\n", &st[0]);
    printf("%p\n", &(a[0]));
    printf("%c\n", *st);
    printf("%p", &a);
}
Run Code Online (Sandbox Code Playgroud)

这是我得到的输出

0x1265028
0x1265028
h
0x7ffe26a91c40
Run Code Online (Sandbox Code Playgroud)

如果我的理解是正确&a的应该返回字符串的地址,为什么返回的值与&a其余的不同?

c++ pointers stdstring memory-address

1
推荐指数
2
解决办法
152
查看次数

标签 统计

c++ ×1

memory-address ×1

pointers ×1

stdstring ×1