函数: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;
}

一、memcmp含义

Compare characters in two buffers.

int memcmp( const void* buf1, const void* buf2, size_t count );inline int wmemcmp ( const wchar_t* buf1, const wchar_t* buf2, size_tcount);

Parameters

buf1 : First buffer.
buf2 : Second buffer.
count : Number of characters.
Return Values : The return value indicates the relationship between the buffers.
Return Value Relationship of First count Bytes of buf1 and buf2
< 0 buf1 less than buf2
0 buf1 identical to buf2

> 0

buf1 greater than buf2

二、memcmp与strcmp的区别

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; 
 }

int strncmp(const char * cs,const char * ct,size_t count) 

register signed char __res = 0; 
while (count) { 
if ((__res = *cs - *ct++) != 0 || !*cs++) 
break; 
count--; 

return __res; 
}

1、这两个函数的差别其实还是挺大的,差别在这里: 
对于memcmp(),如果两个字符串相同而且count大于字符串长度的话,memcmp不会在\0处停下来,会继续比较\0后面的内存单元,直到_res不为零或者达到count次数。 
对于strncmp(),由于((__res = *cs - *ct++) != 0 || !*cs++)的存在,比较必定会在最短的字符串的末尾停下来,即使count还未为零。具体的例子: 
char a1[]="ABCD"; 
char a2[]="ABCD"; 
对于memcmp(a1,a2,10),memcmp在两个字符串的\0之后继续比较 
对于strncmp(a1,a2,10),strncmp在两个字符串的末尾停下,不再继续比较。 
所以,如果想使用memcmp比较字符串,要保证count不能超过最短字符串的长度,否则结果有可能是错误的。

2、strncmp("abcd", "abcdef", 6) = 0。比较次数是一样的: 
memcmp:在比较到第5个字符也就是'\0',*su1 - *su2的结果显然不等于0,所以满足条件跳出循环,不会再进行后面的比较。我想在其他情况下也一样。
strncmp:同样的道理再比较到第5个字符时结束循环,其实strncmp中“!*cs++”完全等同于“!*ct++”,其作用仅在于当两个字符串相同的情形下,防止多余的比较次数。

memcmp与strncmp函数【转】的更多相关文章

  1. strncmp函数——比较特定长度的字符串

    strncmp函数用于比较特定长度的字符串. 头文件:string.h. 语法  int strncmp(const char *string1, const char *string2, size_ ...

  2. php strncmp()函数 语法

    php strncmp()函数 语法 作用:比较字符串前n个字符,区分大小写 语法:strncmp(string1,string2,length)直线电机品牌 参数: 参数 描述 string1 必须 ...

  3. PHP strncmp() 函数

    实例 比较两个字符串(区分大小写): <?php高佣联盟 www.cgewang.comecho strncmp("Hello world!","Hello ear ...

  4. 字符串函数---strcmp()与strncmp()具体解释及实现

    一.strcmp()与strncmp() strcmp():strcmp(s1,s2);            比較两个字符串.        strncmp():strncmp(s1,s2);   ...

  5. 字符串函数---strcmp()与strncmp()详解及实现【转】

    本文转载自:http://blog.csdn.net/lanzhihui_10086/article/details/39829623 一.strcmp()与strncmp() strcmp():st ...

  6. C string 函数大全

    PS:本文包含了大部分strings函数的说明,并附带举例说明.本来想自己整理一下的,发现已经有前辈整理过了,就转了过来.修改了原文一些源码的问题,主要是用char *字义字符串的问题,导致程序运行时 ...

  7. 彻底弄清c标准库中string.h里的常用函数用法

    在我们平常写的c/c++程序,一些算法题中,我们常常会用到c标准库中string.h文件中的函数,这些函数主要用于处理内存,字符串相关操作,是很有用的工具函数.而且有些时候,在笔试或面试中也会出现让你 ...

  8. 头文件string.h中的函数及使用方法

    来源:http://blog.csdn.net/tsyj810883979/article/details/5116817 字符串拷贝1 @函数名称:   strdup函数原型:   char *st ...

  9. c语言string的函数

    PS:本文包含了大部分strings函数的说明,并附带举例说明.本来想自己整理一下的,发现已经有前辈整理过了,就转了过来.修改了原文一些源码的问题,主要是用char *字义字符串的问题,导致程序运行时 ...

随机推荐

  1. How to Add Trust Sites into IE before IE10 through Group Policy

    Due to IE10 published, I'll conclude the methods that how to add trust sites in to IE of the version ...

  2. py3+urllib+re,轻轻松松爬取双色球最近100期中奖号码

    通过页面源码,发现使用正则表达式可以很方便的获取到我们需要的数据,最后循环写入txt文件. (\d{2})表示两位数字 [\s\S]表示匹配包括“\r\n”在内的任何字符,匹配红球和蓝球之间的内容 具 ...

  3. CDQZ多校集训记

    20171218 DAY0 初相逢 今天的阳光很好,确实好极了.下午开始时,mercer说门外站了一堆人,我看都不用看就知道是衡水的.衡水人,怎么说呢,觉得还是挺不一样的.不知道像凡哥和超哥这种奇异的 ...

  4. 【CSS】定义元素的对齐方式

    1.文本内容居中对齐:text-align.扩展用法:父元素嵌套子元素时,且子元素的宽度小于父元素宽度,使用text-align:center,可以实现子元素的居中对齐. <!DOCTYPE h ...

  5. Web Scraping with Python

    Python爬虫视频教程零基础小白到scrapy爬虫高手-轻松入门 https://item.taobao.com/item.htm?spm=a1z38n.10677092.0.0.482434a6E ...

  6. Ansible拷贝文件遇到的问题

    ansible报错Aborting, target uses selinux but python bindings (libselinux-python) aren't installed 报错内容 ...

  7. 深入分析HashMap

    本文基于jdk1.8 HashMap特点: HashMap具体方法分析: put方法分析: 执行流程图: public V put(K key, V value) { return putVal(ha ...

  8. Bandicam录制视频

    我已经录制了一个视频,关于怎么录制视频的,原画画质的嘻嘻.视频地址  http://www.tudou.com/programs/view/T7xzG1CgsD4 ------------------ ...

  9. css 中ul li 与 ul>li的区别

    ul li :后代选择器,ul 里所有的 li 元素,包括 ol 里的 li ; ul>li :子代选择器,下一级的DOM节点,不包括 ol 里的 li . ul>ol>li :子代 ...

  10. Access restriction: The constructor SunJCE() is not accessible 错误

    Access restriction: The type 'SunJCE' is not API (restriction on required library 'C:\Program Files\ ...