r**d 发帖数: 116 | 1 下面是apple的source code. 我不明白为什么需要把char 转换成unsigned char?
strcmp(const char *s1, const char *s2)
{
for ( ; *s1 == *s2; s1++, s2++)
if (*s1 == '\0')
return 0;
return ((*(unsigned char *)s1 < *(unsigned char *)s2) ? -1 : +1);
} | S**I 发帖数: 15689 | 2 In the return statement, what if *s1 is '\0' and *s2 is a signed char with
negative value? the function sould return -1 in this case, but it will
return +1 instead.
【在 r**d 的大作中提到】 : 下面是apple的source code. 我不明白为什么需要把char 转换成unsigned char? : strcmp(const char *s1, const char *s2) : { : for ( ; *s1 == *s2; s1++, s2++) : if (*s1 == '\0') : return 0; : return ((*(unsigned char *)s1 < *(unsigned char *)s2) ? -1 : +1); : }
| k*******a 发帖数: 433 | 3 if they are ASCII code, char* is ok
if they are extended ASCII code, must be unsigned char * |
|