注册 登录
编程论坛 C++教室

[求助]这样遍历字符串为什么不对呢?

alleks 发布于 2007-08-03 00:58, 887 次点击

char* p=we are friends.;

char* q=p;

while(q)

{

cout<<*q;

q++;

}
请问这样遍历字符串为什么不对呢?
书上不是说,字符串相当于一个末尾是空字符的char数组吗?
请指教,谢谢!
2 回复
#2
企鹅2007-08-03 02:26

char* p=”we are friends.”;

char* q=p;

while(*q)

{

cout<<*q;

q++;

}

你的想法没错但是while里面你放的是/0的地址,不是/0 所以错了

#3
alleks2007-08-03 09:54
哦,是这样的,是我疏忽了
谢谢楼上的啊:)
1