posts - 297,  comments - 15,  trackbacks - 0
函数:int memcmp (const void *a1, const void *a2, size_t size) 
       函数memcmp用于比较字符串s1与s2的前size个字符。
      如果两上字符块相同,memcmp将返回0。

函数:int strcmp (const char *s1, const char *s2) 
        这个函数用来比较s1和s2字符串,这个函数将返回一个值,它的符号与第一对不同的字符的比较结果相关。
       如果两个字符串相等的话,strcmp将返回0。
       如果s1是s2的一个子串的话,s1小于s2
此外还有函数 
    int strncmp (const char *s1, const char *s2, size_t size) 
    此函数与strcmp极为类似。不同之处是,strncmp函数是指定比较size个字符。也就是说,如果字符串s1与s2的前size个字符相同,函数返回值为0。

功能比较:

     二者都可以用于字符串的比较,但是二者是有比较大的差异的,因为strcmp是按照字节(byte-wise)比较的,并且比较的过程中会检查是否出现了"\0"结束符,一旦任意一个字符串指针前进过程中遇到结束符,将终止比较。而memcmp函数是用于比较两个内存块的内容是否相等,在用于字符串比较时通常用于测试字符串是否相等,不常进行byte-wise的字符串比较。如果要比较的对象中包含一些由于边界对齐需求而填入结构对象中的空格、联合 (union)结束的额外空格、字符串所分配的空间未使用完的部分引起的“holes”的话,最好使用memcmp来完成。这些“holes”的内容是不确定的,在执行byte-wise比较时结果也是不明确的。

效率差异:
     strcmp比较的字符串,而memcmp比较的是内存块,strcmp需要时刻检查是否遇到了字符串结束的 \0 字符,而memcmp则完全不用担心这个问题,所以memcmp的效率要高于strcmp

使用示例:

给出一个如下的结构定义:
struct foo
  {
    unsigned char tag;
    union
       {
         double f;
        long i;
          char *p;
       } value;
  };
      如果要比较两个struct foo对象的话,建议最好使用memcmp。
     在给出一个字符串比较的例子,判断字符串str中前四个中字符是否为 0x80100001,因为0x00对于字符串而言,这是个结束符,如果使用strncmp的话strncmp(str,"\x80\x10\x00 \x01",4)的话,实际效果是只判断了是否含有0x8010,也就是说一旦str中前两个字符为0x8010就返回0,表示相同了,显然这是不正确的!此时应该使用memcmp(str,"\x80\x10\x00\x01",4),这样一来就达到了目的

 附:strcmp,strncmp,memcmp的Linux的源代码

/**
 * strcmp - Compare two strings
 * @cs: One string
 * @ct: Another string
 */
int strcmp(const char *cs, const char *ct)
{
        signed char __res;

        while (1) {
                if ((__res = *cs - *ct++) != 0 || !*cs++)
                        break;
        }
        return __res;
}

 /**
 * strncmp - Compare two length-limited strings
 * @cs: One string
 * @ct: Another string
 * @count: The maximum number of bytes to compare
 */
int strncmp(const char *cs, const char *ct, size_t count)
{
        signed char __res = 0;

        while (count) {
                if ((__res = *cs - *ct++) != 0 || !*cs++)                  //比较到结束符\0,时,已经做了__res = *cs - *ct了,所以不等长度时,肯定返回不为0
                        break;
                count--;
        }
        return __res;
}
/**
 * memcmp - Compare two areas of memory
 * @cs: One area of memory
 * @ct: Another area of memory
 * @count: The size of the area.
 */
int memcmp(const void *cs, const void *ct, size_t count)
{
        const unsigned char *su1, *su2;
        int res = 0;
        for (su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--)
                if ((res = *su1 - *su2) != 0)
                        break;
        return res;
}

转自:

http://blog.chinaunix.net/u2/67780/showart_2079171.html


posted on 2009-11-24 12:04 chatler 阅读(1829) 评论(0)  编辑 收藏 引用 所属分类: C++_BASIS

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


<2024年3月>
252627282912
3456789
10111213141516
17181920212223
24252627282930
31123456

常用链接

留言簿(10)

随笔分类(307)

随笔档案(297)

algorithm

Books_Free_Online

C++

database

Linux

Linux shell

linux socket

misce

  • cloudward
  • 感觉这个博客还是不错,虽然做的东西和我不大相关,觉得看看还是有好处的

network

OSS

  • Google Android
  • Android is a software stack for mobile devices that includes an operating system, middleware and key applications. This early look at the Android SDK provides the tools and APIs necessary to begin developing applications on the Android platform using the Java programming language.
  • os161 file list

overall

搜索

  •  

最新评论

阅读排行榜

评论排行榜