C语言之memcmp函数

【FROM MSDN && 百科】

原型:  int memcmp(const void *buf1, const void *buf2, unsigned int count);

#include<string.h>

比较内存区域buf1和buf2的前count个字节。此函数是按字节比较。

Compares the first num bytes of the block of memory pointed by ptr1 to the first num bytes pointed by ptr2, returning zero if they all match or a value different from zero representing which is greater if they do not。

Notice that, unlike strcmp, the function does not stop comparing after finding a null character.


对于memcmp(),如果两个字符串相同而且count大于字符串长度的话,memcmp不会在\0处停下来,会继续比较\0后面的内存单元,如果想使用memcmp比较字符串,要保证count不能超过最短字符串的长度,否则结果有可能是错误的。


DEMO:

[cpp]  view plain  copy
  1. //#define FIRST_DEMO  
  2. #define MYMEMCMP  
  3. #ifdef FIRST_DEMO  
  4. #include <stdio.h>  
  5. #include <conio.h>  
  6. #include <string.h>  
  7. int main(void)  
  8. {  
  9.     char *s1="Hello, Programmers!";  
  10.     char *s2="Hello, Programmers!";  
  11.     int r;  
  12.     r=memcmp(s1,s2,50/*strlen(s1)*/);  
  13.     if (!r)  
  14.     {  
  15.         printf("s1 and s2 are identical!\n");  
  16.     }  
  17.     else if (r<0)  
  18.     {  
  19.         printf("s1 less than s2\n");  
  20.     }  
  21.     else  
  22.     {  
  23.         printf("s1 greater than s2\n");  
  24.     }  
  25.     getch();  
  26.     return 0;  
  27. }  
  28. #elif defined MYMEMCMP  
  29. #include <stdio.h>  
  30. #include <conio.h>  
  31. #include <string.h>  
  32. int mymemcmp(const void *buffer1,const void *buffer2,int ccount);  
  33. void Print(char *str1,char *str2,int t);  
  34. int main(void)  
  35. {  
  36.     char *str1="hel";  
  37.     char *str2="hello";  
  38.     Print(str1,str2,mymemcmp(str1,str2,3));  
  39.     Print(str2,str1,mymemcmp(str2,str1,3));  
  40.     Print(str2,str2,mymemcmp(str2,str2,3));  
  41.     getch();  
  42.     return 0;  
  43. }  
  44. /*FROM:http://blog.chinaunix.net/uid-20480343-id-1941630.html */  
  45. int mymemcmp(const void *buffer1,const void *buffer2,int count)  
  46. {  
  47.     if (!count)  
  48.     {  
  49.         return 0;  
  50.     }  
  51.     while(count && *(char *)buffer1==*(char *)buffer2)  
  52.     {  
  53.         count--;  
  54.         buffer1=(char *)buffer1-1;  
  55.         buffer2=(char *)buffer2-1;  
  56.     }  
  57.     return (*((unsigned char *)buffer1)- *((unsigned char *)buffer2));  
  58.   
  59. }  
  60.   
  61. void Print(char *str1,char *str2,int t)  
  62. {  
  63.     if (t>0)  
  64.     {  
  65.         printf("\n%s Upper than %s \n",str1,str2);  
  66.     }  
  67.     else if(t<0)  
  68.     {  
  69.         printf("\n%s Lower than %s \n",str1,str2);  
  70.     }  
  71.     else  
  72.     {  
  73.         printf("\n%s equal %s \n",str1,str2);  
  74.     }  
  75. }  
  76. #endif  
memcmp按字节比较,可以设置比较的位数
strcmp按字符比较,只能比较整个字符串
都是用ASCII码进行比较,效率在数量级上不会相差太大的

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

sunxiaopengsun

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值