欢迎光临
我们一直在努力

memcmp(),memcmp的返回值

memcmp函数: int memcmp (const void *s1, const 便宜美国vps void *s2, size_t n);

这个函数用来比较 s1 和 s2 所指的内存区间前 n 个字符。

第一个字符串大于第二个字符串,则返回大于0的数字;

第一个字符串等于第二个字符串,则返回0;

第一个字符串小于第二个字符串,则返回小于0的数字。

代码: #include<stdio.h>#include<assert.h>int my_memcmp(const void* buf1, const void* buf2, size_t count){ assert(buf1 != NULL && buf2 != NULL); const char* pf1 = (const char*)buf1; const char* pf2 = (const char*)buf2; int res = 0; while (count– != 0) { res = *pf1 – *pf2; if (res != 0) break; pf1++; pf2++; } return res;}void main(){ char str1[20] = “hello”; char str2[20] = “helloworld”; int a = my_memcmp(str1, str2, 3); printf(“%d”, a);} 00771004

赞(0)
【声明】:本博客不参与任何交易,也非中介,仅记录个人感兴趣的主机测评结果和优惠活动,内容均不作直接、间接、法定、约定的保证。访问本博客请务必遵守有关互联网的相关法律、规定与规则。一旦您访问本博客,即表示您已经知晓并接受了此声明通告。