请考虑以下代码:
char arr[3][10]={"white","red","blue"};
cout<<arr[2]; //output 1
int brr[3][3]={1,2,3,4,5,6,7,8,9};
cout<<brr[2]; //output 2
cout<<*brr[2];
输出1:白色,解引用后给出w作为输出
输出2:十六进制地址(0x28 .....)
输出3:最后一行的第一个元素7
我很困惑为什么整数和字符数组的输出存在差异。
提供指针时,它将使用std::ostream& operator<<( const void* value );
重载并输出由指向的地址value
。
区别在于这std::ostream& operator<<(std::ostream& os, const char* s)
是一种特殊情况,它将取消引用s
并输出char
s直到\0
遇到a。只要不\0
遇到任何问题,它就会s
向前迈进。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句