这段代码输出一个随机内存地址,任何人都可以解释为什么以及如何解释吗?
#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/