int和char数组之间有什么区别? [英] What is the difference between int and char arrays?
问题描述
下面的int和char数组有什么区别:
What is the difference between int and char arrays below:
int main()
{
int numbers[] = {2,1,3};
char letter[] = {'a','b','\0'};
cout<< numbers<<endl;
cout<< letter<<endl;
}
输出:
0x22ff12 // an address
ab
是不是213显示?
我知道数组的名称将指向它的第一个元素的地址,但是为什么
显示不同的行为?
Why isn't the 213 displayed ? I know the name of an array will point to the address of its first element, but why does a char array display different behavior?
推荐答案
没有运算符<<
重载需要数组,所以你传递的参数例如 numbers
和 letter
)进行数组到指针转换, void * code>和
char *
。
There is no operator<<
overload that takes arrays, exactly, so the arguments you pass (eg numbers
and letter
) undergo array-to-pointer conversion, to void*
and char*
respectively.
有一个 ;<()
,它接受一个 const void *
,另一个接受 const char *
。当您调用:
There is an overload of operator<<()
that takes a const void*
, and another that takes a const char*
. When you call:
cout<< numbers<<endl;
const void *
但是当你调用:
cout<< letter<<endl;
匹配 const char *
版本。
在 const void *
版本中,显示指针,而 const char *
版本,字符串显示到空终止符。
In the const void*
version, the pointer is displayed, while with the const char*
version, the string is displayed up to the null terminator.
这篇关于int和char数组之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!