c语言练习题53:模拟实现memmove

模拟实现memmove

c语言练习题53:模拟实现memmove_第1张图片c语言练习题53:模拟实现memmove_第2张图片

c语言练习题53:模拟实现memmove_第3张图片

#include
#include
void* my_memmove(void* dest, const void* src, size_t num)
{
	void* ret = dest;
	assert(dest != NULL);
	assert(src != NULL);
	if (dest < src) {//前相后
		while (num--) {
			*(char*)dest=*(char*)src;
			dest = (char*)dest + 1;
			src  = (char*)src + 1;
		}
	}
	else {//后相前
		//num=19
		while (num--) {
			*((char*)dest + num) = *((char*)src + num);
		}
	}
	return ret;
}
int main() {
	int arr[] = { 1,2,3,4,5,6,7,8,9,10 };
	my_memmove(arr+2, arr, 20);
	int i = 0;
	for (i = 0; i < 10; i++) {
		printf("%d ", arr[i]);
	}
	return 0;
}

c语言练习题53:模拟实现memmove_第4张图片

你可能感兴趣的:(c语言,算法,数据结构)