字符数组和int数组的不同

#include<iostream>
using namespace std;
void main()
{
	char a[]="hello";
	char b[]="hello";
	cout<<a<<'\n'<<*b<<endl;
}

猜猜打印输出什么?

hello

h

 

#include<iostream>
using namespace std;
void main()
{
	int a[]={4,5,6};
	int b[]={4,5,6};
	cout<<a<<'\n'<<*b<<endl;
}

猜猜打印输出什么?

0018FF38

4

 

这说明int数组和字符数组在输出机制上是不同的。

另外,字符数组"hello"在电脑中会自动变成"hello\0"

所以

char s[5]="hello"; 会报错

但是

char s[5]={'h','e','l','l','o'};不会报错,但是输出会有问题

#include<iostream>
using namespace std;
void main()
{
    char a[5]={'h','e','l','l','o'};
    char b[5]={'h','e','l','l','o'};
    cout<<a<<'\n'<<*b<<endl;
}

上面的输出大概是系统去寻找'\0',把找到之前的经过的数据都输出造成的。

posted @ 2013-06-30 17:43  wkl7123  阅读(244)  评论(0)    收藏  举报