【C语言】【笔试题】模拟实现memcmp
生活随笔
收集整理的這篇文章主要介紹了
【C语言】【笔试题】模拟实现memcmp
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
#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};//清楚內存是怎樣放置的int?ret?=?my_memcmp2(arr1,?arr2,?16);printf("%d",?ret);system("pause");return?0;
}
轉載于:https://blog.51cto.com/10740329/1715699
總結
以上是生活随笔為你收集整理的【C语言】【笔试题】模拟实现memcmp的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 在线白板,基于socket.io的多人在
- 下一篇: 需求工程-软件需求模式读书笔记3