Simulate the implementation of memcmp

Go directly to the source code

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;//若上面及不大于也不小于则一定是相等
}

Guess you like

Origin blog.csdn.net/ZYK069/article/details/129031826