memcmp(),memcmp的返回值

2022年 11月 8日 发表评论
腾讯云正在大促:点击直达 阿里云超级红包:点击领取
免费/便宜/高性价比服务器汇总入口(已更新):点击这里了解
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

小咸鱼

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: