gpt4 book ai didi

c++ - 为什么指针返回值而不是地址?

转载 作者:太空宇宙 更新时间:2023-11-04 14:38:10 25 4
gpt4 key购买 nike

这段代码:

string  str1 ( "Hello world" );
const char *c_str1 = str1.c_str ( );
cout << "The C-style string c_str1 is: " << c_str1

生成此输出:

The C-style string c_str1 is: Hello world

我不明白。

c_str1 是一个指针,对吧?因此,c_str1 应该返回一个地址,只有 *c_str1 应该给出位于该地址的值。但是,在上面的示例中,c_str1 给出了值(不是地址)。

我误解了什么?

最佳答案

这是因为std::cout::operator <<已定义 - 它有一个需要 const char* 的重载并打印它指向的字符串。如果你想要地址,你必须转换为 void* .

关于c++ - 为什么指针返回值而不是地址?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15900615/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com