/*
练习3.37:
下面的程序是何含义,程序的输出结果是什么?
const char ca[]={'h', 'e', 'l', 'l', 'o'};
const char *cp=ca;
while(*cp){
cout<<*cp<<endl;
++cp;
}
答:ca是一个常量字符数组,但是结尾没有含空字符'\0',
cp是一个指向常量字符的指针,初始化值为一个常量字符数组第一个元素的地址
while循环的条件是:cp指向的字符不为空,循环体:先输出cp指向的字符,cp向前移动,指向下一下字符。
程序不仅输出字符数组的字符,还向接着输出别的内容,直到遇到空字符才停止输出。
*/
#include <iostream>
#include <string>
#include <typeinfo>
#include <vector>
#include <cctype>
#include <cstring>
using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::vector;
int main()
{
const char ca[]={'h', 'e', 'l', 'l', 'o'};
const char *cp=ca;
while(*cp){
cout<<*cp<<endl;
++cp;
}
cout<<endl;
return 0;
}
C++Primer第五版第三章练习3.37
于 2023-12-22 15:59:34 首次发布