memmove函数的模拟实现--C语言

memmove是一个可以实现各种类型的复制的函数。与其他有类似功能的函数相比,这个函数可以在两个目标内存有重复情况下,很容易地出错。
该函数的原型是: void *memmove(void *dest, const void *src, unsigned int count);
参数说明:dest:是目标内存的地址。
src:被复制内容的内存地址。
count:需要复制的字节数。
具体实现如下:

#include<stdio.h>
#include<windows.h>
#pragma warning(disable:4996)
void * mymemmove(void *dst, const void *src, int l){
	void *ret = dst;
	char *dst_t = (char *)dst;
	const char *src_t = (char *)src;
	int i = 0;
	if (l == 0){
		return NULL;
	}
	if (dst == NULL||src == NULL){
		return NULL;
	}
	else if (dst_t > src_t&&dst_t<src_t+l){
		
		while (l){ 
			 *(dst_t + l - 1) = * (src_t + l - 1);
			l--;
		}
		return ret;
	}
	else{
		while (i<l){
		*(dst_t + i) = *(src_t + i);
		i++;
	}
		return ret;
	}
}
int main(){
	char str[] = "abcdefg";
	char str1[] = "xyz";
	int s = 5;
	int *a = &s;
	mymemmove(str+1, str, 4);
	printf("%s\n", str);
	system("pause");
	return 0;
}

如有错误,欢迎指正。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值