赋值篇:
part1 错误引例*2:
1 char c6[];//wrong 2 c6="hello";//wrong 3 4 char c7[];//wrong 5 c7[0]='h';//wrong 6 c7[1]='e';//wrong 7 c7[2]='l';//wrong 8 c7[3]='l';//wrong 9 c7[4]='o';//wrong
1.不允许声明之后才对整个字符数组进行赋值,比如c6
2.除非声明时顺便赋值了,否则不允许不声明数组长度,比如c7
part2:
1 char c1[]={'h','e','l','l','o'}; 2 char c2[]={'h','e','l','l','o',' '}; 3 char c3[10]={'h','e','l','l','o',' '}; 4 printf("c1:%s sizeofc1:%dn",c1,sizeof(c1)); 5 printf("c2:%s sizeofc2:%dn",c2,sizeof(c2)); 6 printf("c3:%s sizeofc3:%dn",c3,sizeof(c3));
1.printf %s时,每次地址+1,直到找到’n’为止,所以c1会乱码
part3:
1 char *c4="hello"; 2 char *c5="hello "; 3 char *c6; 4 c6="hello"; 5 printf("c4:%s sizeofc4:%dn",c4,sizeof(c4)); 6 printf("c5:%s sizeofc5:%dn",c5,sizeof(c5)); 7 printf("c6:%s sizeofc6:%dnn",c6,sizeof(c6));
1.注意字符指针可以声明后赋值,与第一个c6对比
2.注意这里打印的size,因为打印的是字符串数组的首字地址size,所以size都是一样的
part4:
1 char c7[]="hello"; 2 char c8[]="hello ";
1.注意用字符串常量赋值会自动补’