[英]Different behavior of character and integer array in c++
这是c ++中的一个小代码,我分别创建了两个char和int数据类型的数组。但是,对于这两个数组,相同的打印操作表现不同
#include<iostream>
using namespace std;
int main()
{
char a[5]={'h','e','l','l','o'};
int b[5]={1,2,3,4,5};
cout<<a; //displays the string "hello"
cout<<"\n"<<b; //displays the address of b[0]
return(0);
}
我期望输出是两个数组的第一个元素的地址,即a [0]和b [0]的地址,但是在这种情况下,char类型数组的行为不同。
对于cout
,它是一个特殊的重载操作符,它将char *
参数视为空终止字符串并打印整个字符串。
如果要打印地址,请将其转换为void *
。
cout << (void *) a;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.