char是一个字节,那char数组是怎么存储多字节的汉字的呢?
在windows10 用vs2017编译下面程序
#include<iostream>
using namespace std;
int main()
{
char s[] = "汉字";
cout << sizeof(s) << endl;
cout << s << endl;
system("pause");
return 0;
}
得到
查询内存分布后得到
而在gbk编码下
从这些可以知道,多字节编码的汉字存储在char数组里时被截断了。
sizeof()得到的结果可能因为编码的不同而不同
至于本例中为什么等于5的原因是后面还有个’\0’