C++的基础问题,请高手指点迷津!

StarLee 2005-12-30 09:34:29
如下程序段输出什么?为什么?

const char *p = "Test";

cout << &p << endl;
cout << p << endl;
cout << *p << endl;

p = "Other";

cout << &p << endl;
cout << p << endl;
cout << *p << endl;
...全文
142 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
StarLee 2006-01-17
  • 打赏
  • 举报
回复
在“我的问题”直接点击链接,打不开,用“管理”却能打开,回复一个试试!
StarLee 2006-01-17
  • 打赏
  • 举报
回复
在“我的问题”直接点击链接,打不开,用“管理”却能打开,回复一个试试!
StarLee 2006-01-17
  • 打赏
  • 举报
回复
在“我的问题”直接点击链接,打不开,用“管理”却能打开,回复一个试试!
StarLee 2005-12-30
  • 打赏
  • 举报
回复
为什么这样的代码输出的却是q的地址呢?

int i = 13;
int *q = &i;

cout << q <<endl;

这跟上面的有什么不同呢?
hiqingqing 2005-12-30
  • 打赏
  • 举报
回复
同意是重载
aniude 2005-12-30
  • 打赏
  • 举报
回复
我把const去掉,两次输出的地址还是一样,为什么?
====>
char *p = "Test";
其实这里p指向的就是常量,就算你不加const系统也会默认的

另外p是地址,存放"Test"的首地址,&p自然是系统分配给p的地址,即指针p的地址
piaochen_2002 2005-12-30
  • 打赏
  • 举报
回复
template <class E, class T = char_traits<E> >
class basic_ostream {
public:
class sentry;
explicit basic_ostream(basic_streambuf<E, T> *sb);
virtual ~ostream();
bool opfx();
void osfx();
basic_ostream& operator<<(basic_ostream& (*pf)(basic_ostream&));
basic_ostream& operator<<(basic_ios<E, T>& (*pf)(basic_ios<E, T>&));
basic_ostream& operator<<(ios_base<E, T>& (*pf)(ios_base<E, T>&));
basic_ostream& operator<<(basic_streambuf<E, T> *sb);
basic_ostream& operator<<(const char *s);
basic_ostream& operator<<(char c);
basic_ostream& operator<<(bool n);
basic_ostream& operator<<(short n);
basic_ostream& operator<<(unsigned short n);
basic_ostream& operator<<(int n);
basic_ostream& operator<<(unsigned int n);
basic_ostream& operator<<(long n);
basic_ostream& operator<<(unsigned long n);
basic_ostream& operator<<(float n);
basic_ostream& operator<<(double n);
basic_ostream& operator<<(long double n);
basic_ostream& operator<<(void * n);
basic_ostream& put(E c);
basic_ostream& write(E *s, streamsize n);
basic_ostream& flush();
pos_type tellp();
basic_ostream& seekp(pos_type pos);
basic_ostream& seekp(off_type off, ios_base::seek_dir way);
};
这个是标准模板basic_ostream ,cout是继承这个模板的.也继承了它对<<的操作
piaochen_2002 2005-12-30
  • 打赏
  • 举报
回复
cout<<&p这个&p是系统给p分配的地址,所以是不变的,如同这样: int i=1;i=2;&i的值也不会改变.
cout<<p之所有会输出p指向的内容是因为cout对操作符<<的重载吧.
StarLee 2005-12-30
  • 打赏
  • 举报
回复
我把const去掉,两次输出的地址还是一样,为什么?

还有就是p是一个指针,为什么输出p的时候输出的是p指向的内容呢?
jianwang_yz 2005-12-30
  • 打赏
  • 举报
回复
const char *p = "Test";

cout << &p << endl; // 输出 p的地址;因为&就是取地址啊
cout << p << endl; // 输出 p指向的字符串常量 Test;这个显然的啦,p是字符串指针嘛
cout << *p << endl; // 输出 p指向的字符串常量的第一个字符:T。因为p是指针,它指向字符串,但在p的内存单元里实际存放的就是第一个字符的地址(跟数组名类似),所以*p就是该地址所指向的值,即T

p = "Other";

cout << &p << endl; //输出 p的地址;而且与上面的内存地址一样,因为p是指针常量(有const定义)
cout << p << endl; //输出 p指向的字符串常量 Other,同上
cout << *p << endl; //输出 p指向的字符串常量的第一个字符:O。同上
piaochen_2002 2005-12-30
  • 打赏
  • 举报
回复
&p是p的地址,
p里面放的值是字符串常量的首地址!
herman~~ 2005-12-30
  • 打赏
  • 举报
回复
const char *p = "Test";
是静态变量,我的理解是:对象只初始化一次,所以第二次赋值的机器地址没有更换
piaochen_2002 2005-12-30
  • 打赏
  • 举报
回复
const char *p = "Test";

cout << &p << endl; // 输出 p的地址 我的机器上是0012ff7c
cout << p << endl; // 输出 p指向的字符串常量 test
cout << *p << endl; // 输出 p指向的字符串常量的第一个字符。 t

p = "Other";

cout << &p << endl; //输出 p的地址 我的机器上是0012ff7c
cout << p << endl; //输出 p指向的字符串常量 Other
cout << *p << endl; //输出 p指向的字符串常量的第一个字符。 O
xupy520 2005-12-30
  • 打赏
  • 举报
回复
<<对于char * 和 int * 的重载是不一样的吧
piaochen_2002 2005-12-30
  • 打赏
  • 举报
回复
一样的问题,继承了,basic_ostream 关于<<的方法

33,311

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧