您的位置:首页 > 编程语言 > C语言/C++

C语言中内存操作函数memcmp

2014-01-16 09:06 239 查看
六、memcmp

名称:
memcmp
功能:
比较两个内存空间的字符
头文件:
#include <stdlib.h>
函数原形:
int memcmp(void *buf1,void *buf2,unsigned int count);
参数:
buf1 内存区
buf2    内存区
count 要比较的字符数
返回值:
见下面
Memcmp会比较内存区域buf1和buf2的前count个字节.Memcmp回根据ASCLL码表顺序依次比较.当buf1<buf2时,返回<0;当buf1=buf2时,返回0;当buf1>buf2时,返回>0.

main()
{
int *p1=NULL;
int *p2=NULL;
int rt;

p1=malloc(sizeof(int)*10);
if(p1==NULL)
exit(1);
p2=malloc(sizeof(int)*10);
if(p2==NULL)
exit(1);
memset(p1,'a',sizeof(int)*10);
memset(p2,'b',sizeof(int)*10);
rt=memcmp(p1,p2,sizeof(int)*10);
if(rt>0)
printf("p1>p2);
if(rt<0)
printf("p1<p2");
if(rt==0)
printf("p1=p2");

free(p1);
free(p2);
}
运行结果:p1<p2
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: