【C语言】【笔试题】模拟实现memcmp
生活随笔
收集整理的這篇文章主要介紹了
【C语言】【笔试题】模拟实现memcmp
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
#include?<stdio.h>
#include?<stdlib.h>
#include?<assert.h>
int?my_memcmp1(const?void?*p1,?const?void?*p2,?size_t?count)//方法1
{assert(p1);assert(p2);char?*dest?=?(char?*)p1;char?*src?=?(char*)p2;while?(count?&&?(*dest?==?*src)){count--;dest++;src++;}if?(count?==?0)return?0;return?*dest?-?*src;
}
int?my_memcmp2(const?void?*p1,?const?void?*p2,?size_t?count)//方法2
{assert(p1);assert(p2);int?ret?=?0;char?*dest?=?(char?*)p1;char?*src?=?(char*)p2;while?(count?&&?(!(ret?=?(*dest?-?*src)))){dest++;src++;count--;}if?(ret?>?0){return?1;}else?if?(ret?<?0){return?-1;}return?0;}int?main()
{int?arr1[]?=?{1,2,3,4,5,6};int?arr2[]?=?{1,2,3,1000000,5,6};//清楚內(nèi)存是怎樣放置的int?ret?=?my_memcmp2(arr1,?arr2,?16);printf("%d",?ret);system("pause");return?0;
}
轉(zhuǎn)載于:https://blog.51cto.com/10740329/1715699
總結(jié)
以上是生活随笔為你收集整理的【C语言】【笔试题】模拟实现memcmp的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 在线白板,基于socket.io的多人在
- 下一篇: 国产360等浏览器安装完flash升级程