字符数组的定义与初始化
char str[10] = {'I',‘a’,'m'}
若小于10,其余字符串自动定为空字符即(' \0 ')
字符串的表示形式
- 用字符数组形式 char str[] = "I am happy";
- 用字符指针指向一个字符串 char *str = "I am happy";
sizeof与strlen
<span> </span>char s1[10]="n12\\";
<span> </span>char *s2="123456";
<span> </span>cout<<strlen(s1)<<" "<<strlen(s2)<<endl;
<span> </span>cout<<sizeof(s1)<<" "<<sizeof(s2)<<endl;
输出结果为 :
4 6
10 4
\\为转义\
\\为转义\
s2退化为指针,大小为4字节
char s3[] = "0123456789"; sizeof(s3); //11
char s4[100]= "0123456789"; sizeof(s4); //100