C++ 为什么指向char的指针的行为与其他数据类型的指针不同

C++ 为什么指向char的指针的行为与其他数据类型的指针不同,c++,pointers,char,C++,Pointers,Char,我对以下陈述有疑问 int intvalue = 3; int *pointInt = &intvalue; char* p = "string"; cout << pointInt << std::endl; // this will give memory location of intvalue which is ok. cout << p<< std::endl; // why this will give string val

我对以下陈述有疑问

int intvalue = 3;
int *pointInt = &intvalue;

char* p = "string";
cout << pointInt << std::endl;  // this will give memory location of intvalue which is ok.
cout << p<< std::endl; // why this will give string value  rather than memory location of where string is stored?
intvalue=3;
int*pointInt=&intvalue;
char*p=“string”;
cout因为有一个,它假定
char*
是a的第一个元素,并打印出整个字符串

尝试流式传输不是以null结尾的字符串的第一个元素的
char*
,您可以获得很多乐趣:

#include <iostream>
int main()
{
  char c = 'x';
  std::cout << &c << std::endl;
}
#包括
int main()
{
字符c='x';

std::cout关键字运算符重载-iostream实例的另一种方法std::cout
负责字符并以不同的方式进行处理。确切的实现还可能产生“Hello World”

cout
char*
const char*
最常用于指向以空结尾的C样式字符串。标准I/O库在传递这些类型中的一种以插入或提取为目的时会考虑到这一点。这些函数的设计只是为了根据希望插入或提取的常见程度使用这种特殊情况打印出一个C样式的字符串

要获取指针值,可以尝试强制转换为其他指针类型:

std::cout << static_cast<void*>(p) << std::endl;

std::cout这是
operator因为重载operator
我编辑了标题;问题是关于
char*
,而不是指向
char*
。你有一个奇怪的——甚至可以说是未定义的——有趣的概念。@JimBalter一个人可以花几个小时运行该代码,并查看出现的漂亮符号。。。
std::cout << static_cast<void*>(p) << std::endl;