çSTRCMP源$ C $ C [英] c strcmp source code
本文介绍了çSTRCMP源$ C $ C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
int strcmp(const char *s1, const char *s2)
{
int ret = 0;
while (!(ret = *(unsigned char *) s1 - *(unsigned char *) s2) && *s2) ++s1, ++s2;
if (ret < 0)
ret = -1;
else if (ret > 0)
ret = 1 ;
return ret;
}
我回顾从code: HTTP:// WWW .jbox.dk / sanos /源/ lib中/ string.c.html
我想这是有一些问题。
如果的strlen(S2)&GT;的strlen(S1)
,那么 ++ S1
可能超出范围。不幸的是,则函数返回错误。
I suppose that there're some problem.
If strlen(s2)>strlen(s1)
,then ++s1
may beyond the range. Unfortunately, then the function return error.
推荐答案
没有,因为环路只有不断而* s1和* S2是平等的和的* S2不是0有没有这样的问题。如果S1较短,一旦进入到\\ 0在s1的末尾,平等条件将打破和循环将停止。
No, there's no such problem since the loop continues only while *s1 and *s2 are equal and *s2 is not 0. If s1 is shorter, once it gets to the \0 at the end of s1, the equality condition would break and the loop would stop.
这篇关于çSTRCMP源$ C $ C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文