我发现仿生中的memcmp.c是这样的:
30 int memcmp(const void *s1, const void *s2, size_t n)
31 {
32 const unsigned char* p1 = s1;
33 const unsigned char* end1 = p1 + n;
34 const unsigned char* p2 = s2;
35 int d = 0;
36
37 for (;;) {
38 if (d || p1 >= end1) break;
39 d = (int)*p1++ - (int)*p2++;
40
41 if (d || p1 >= end1) break;
42 d = (int)*p1++ - (int)*p2++;
43
44 if (d || p1 >= end1) break;
45 d = (int)*p1++ - (int)*p2++;
46
47 if (d || p1 >= end1) break;
48 d = (int)*p1++ - (int)*p2++;
49 }
50 return d;
51 }
在for循环中,同样的逻辑重复了4次,为什么?可以不重复吗?
谢谢,维克多