在解答这个题时遇到的问题,记录一下吧~
#includeint main(void) { int strcmp1(char* p1, char* p2); char s1[100], s2[100]; scanf("%s", s1);//不能写成s1="boy",s1[100]="boy" scanf("%s", s2); char* p1; char* p2; p1 = &s1[0]; p2 = &s2[0]; printf("%d", strcmp1(p1, p2));return 0; } int strcmp1(char* p1, char* p2) { int i = 0; while (*(p1 + i) == *(p2 + i)) { if (*(p1 +i++) == ' '){//先判断*(p1 +i) == ' ',再i++ return 0; } } return (*(p1 + i) - *(p2 + i)); }
①写成s1="boy";
报错:Array type 'char[100]' is not assignableclang(typecheck_array_not_modifiable_lvalue)
原因:s1="boy";这里的s1是指针,赋值“boy”,出现指针混乱。
②写成s1[100]="boy";
报错:Array index 100 is past the end of the array (which contains 100 elements)clang(-Warray-bounds)
原因:这里的s1[100]是指一个字符,而数组的下标是从0开始的,s1[100]不存在,下标最大为99



