字符串文字的 C++ 引用

标签 c++

这段代码输出一个随机内存地址,任何人都可以解释为什么以及如何解释吗?

#include<iostream>
using std::cout;

int main()
{
    cout<<&"hello";
    return 0;
}

output:

0x560d6984e048

...Program finished with exit code 0
Press ENTER to exit console.

最佳答案

C++ 中的文字字符串实际上是常量字符数组(包括空终止符)。

通过使用指针运算符,您可以获得指向该数组的指针。

这在某种程度上相当于这样的事情:

#include <iostream>

char const hello_str[] = "hello";

int main()
{
    std::cout << &hello_str;
}

关于字符串文字的 C++ 引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69438003/

相关文章:

c++ - 如何创建 LPOLESTR 类型的变量或常量?

c++ - 使用 `auto` 作为函数参数是否违反 C++ 标准?

c++ - 虚拟继承会增加派生类的大小吗?

c# - Silverlight 与 XNA 对比纯 HTML5

c++ - 使用 'delete' 时出现语法错误

c++ - (核心转储)C++ 中的链表

c++ - CMake - 将选项集成到 C++ 源文件中

c++ - 什么是 CTime(有没有)32 位模拟?

c++ - 在 .cpp 中没有实现的 Eclipse : how to find functions in . h?

c++ - 如何在打开的程序窗口上自动启用信号