模拟实现memcmp

简介: 模拟实现memcmp

直接上源码

int my_memcmp(const void* ptr1, const void* ptr2, size_t num)
{
  assert(ptr1 && ptr2);
  while (num--)//注意不能写成if eles的形式这样写会导致内容中间存在的一部分相等时就会返回去了
  {
    if (*((char*)ptr1) - *((char*)ptr2) > 0)
      return 1;
    if (*((char*)ptr1) - *((char*)ptr2) < 0)
      return -1;
    ++((char*)ptr1);
    ++((char*)ptr2);
  }
  return 0;//若上面及不大于也不小于则一定是相等
}
相关文章
|
7月前
|
C语言
strlen函数【详解+模拟实现】
strlen函数【详解+模拟实现】
|
7月前
|
安全 C++
内存函数 memcpy 和 memmove 的讲解和模拟实现
内存函数 memcpy 和 memmove 的讲解和模拟实现
30 0
|
5天前
|
编译器 C++
memmove函数和memcpy函数的模拟实现
memmove函数和memcpy函数的模拟实现
11 1
|
5天前
atoi()详解及其模拟实现
atoi()详解及其模拟实现
|
8月前
strstr函数的使用及模拟实现
1.strstr函数 2.strstr函数的使用 3.strstr函数的模拟实现
72 0
|
5天前
atoi函数的模拟实现
atoi函数的模拟实现
|
5月前
模拟实现库函数strlen
模拟实现库函数strlen
15 0
|
7月前
|
存储 安全
memcpy和memmove函数的介绍和模拟实现
memcpy和memmove函数的介绍和模拟实现
|
9月前
模拟实现atoi函数
模拟实现atoi函数
|
10月前
模拟实现atoi
模拟实现atoi
31 0