去面试的时候,笔试题有个这么个题目,问输出值:
#include <iostream>
using namespace std;
int main(void)
{
cout<<(int*)"hello world"<<endl;
system("pause");
return 0;
}结果是指向常量字符串的首地址的值
你懂的,肯定是知道。不懂的就需要来研究下cout<< point *<<endl;
测试发现:
char * p = "hello world";
cout<<p<<endl;
cout<<p[0]<<endl;
cout<<&p[0]<<endl;
输出:
hello world
h
hello world
请按任意键继续. . .
而其它指针 int *, struct *...等都是输出其值的首地址
这个貌似就内部和谐吧,cout还是需要好好去学习的。
本文探讨了使用C++中cout输出字符串与指针的区别,通过示例解释了指向字符串的指针如何被输出为字符串而非地址值的现象。
3296

被折叠的 条评论
为什么被折叠?



