复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15#include <iostream> #include <string> using namespace std; int main() { const char ca[] = {'h','e','l','l','o'}; const char *cp = ca; while (*cp) { cout<<*cp<<endl; ++cp; } return 0; }
代码有问题,ca为并不是以null结尾,所以判断while(*cp)时,会直到一个null才结束,也就是打印出来的内容会多余5个。
while(*cp):判断cp所指内容是否为true,真值表明除null外的任意字符。
稍作修改:
复制代码
即可。系统会自动在字符串末尾添加结束符'',即为null。
1const char ca = "hello";
最后
以上就是可爱自行车最近收集整理的关于Primer 学习 -的全部内容,更多相关Primer内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复