int main() {
short things[] = {1, 5, 3, 8};
int num_elements = sizeof(things) / sizeof(short);
char dog[5] = {'b', 'e', 'a', 'u', 'x'};
char cat[5] = {'f', 'a', 't', 's', '\0'};
int a = 1;
cout << "dog=" <<dog <<endl;
cout << "cat=" <<cat <<endl;
return 0;
}
输出:
/home/justin/code/cplusplus/demo1
dog=beauxfats
cat=fats
两个数组都是char数组,但只有cat数组是字符串,因为dog没有显性‘\0’作为字符串终止;
所以cout dog时,会继续向下寻找,直到遇见\0
另外一种声明方式和存储结果。