C语言之memcmp函数
来源:互联网 发布:网络监测基础与实战 编辑:程序博客网 时间:2024/06/05 06:19
【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:
//#define FIRST_DEMO#define MYMEMCMP#ifdef FIRST_DEMO#include <stdio.h>#include <conio.h>#include <string.h>int main(void){char *s1="Hello, Programmers!";char *s2="Hello, Programmers!";int r;r=memcmp(s1,s2,50/*strlen(s1)*/);if (!r){printf("s1 and s2 are identical!\n");}else if (r<0){printf("s1 less than s2\n");}else{printf("s1 greater than s2\n");}getch();return 0;}#elif defined MYMEMCMP#include <stdio.h>#include <conio.h>#include <string.h>int mymemcmp(const void *buffer1,const void *buffer2,int ccount);void Print(char *str1,char *str2,int t);int main(void){ char *str1="hel";char *str2="hello";Print(str1,str2,mymemcmp(str1,str2,3));Print(str2,str1,mymemcmp(str2,str1,3));Print(str2,str2,mymemcmp(str2,str2,3));getch();return 0;}/*FROM:http://blog.chinaunix.net/uid-20480343-id-1941630.html */int mymemcmp(const void *buffer1,const void *buffer2,int count){if (!count){return 0;}while(count && *(char *)buffer1==*(char *)buffer2){count--;buffer1=(char *)buffer1-1;buffer2=(char *)buffer2-1;}return (*((unsigned char *)buffer1)- *((unsigned char *)buffer2));}void Print(char *str1,char *str2,int t){if (t>0){printf("\n%s Upper than %s \n",str1,str2);}else if(t<0){printf("\n%s Lower than %s \n",str1,str2);}else{printf("\n%s equal %s \n",str1,str2);}}#endif
- C语言之memcmp函数
- C语言之memcmp函数
- C 语言 memcmp函数
- C语言函数_string.h 之 内存comparison函数memcmp
- C语言中内存操作函数memcmp
- 【C语言】memcmp函数的实现
- C语言模拟实现memset.memcmp函数
- 【C语言】 实现memcmp
- C语言memcmp()函数:比较内存前n个字节
- C语言函数memcpy(),memmove(),memcmp()用法详解
- memcmp ( )【C语言库函数源代码】
- 【C语言】【笔试题】模拟实现memcmp
- C操作内存函数(memset,memcpy,memcmp)
- memcmp() 函数
- memcmp() 函数
- memcmp函数
- 函数 memcmp
- memcmp函数
- ios UIView详解~~~~~
- Mysql集群
- 十八年开发经历小结
- iOS UIView非常用方法及属性详解
- ARM中的预取命令pld的使用
- C语言之memcmp函数
- 关于DXImageTransform.Microsoft.AlphaImageLoader
- Hibernate性能优化
- ARP协议的报文格式
- epoll的一些思考
- .“黑监狱”截访案开审 北京法院否认宣判截访人员
- Linux内核编译选项-3
- Hibernate的四种数据加载方式
- 技术人员的出路