指定字符串长度的复制

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
void *memcpy(void *dest,void const *scr,size_t length)
{
	
	char *ret=(char*)dest;
	char *src=(char*)scr;
	int flag=0;
	int n=length;
	
	while(length)
	{
		if(ret==src+1)
		{
			if(flag==0)
			{
				while(length)
				{
					src[length]=src[length-1];
					length--;
				}
				length=n;
				flag=1;
			}
			while(length--)
			{
				*ret++=*(++src);
			}
			*ret='\0';
			return dest;
		}
		else
		{
			*ret++=*src++;
		}
		length--;
	}
	*ret='\0';
	return dest;
}
void main()
{
	char scr[]="abcdefgh";
	char dest[10]={0};
//	char *ret=(char*)memcpy(dest,scr,4);
	char *ret=(char*)memcpy(scr+1,scr,4);
	printf("%s\n",ret);
}
<span style="font-family: Arial, Helvetica, sans-serif;">总结:实现char *ret=(char*)memcpy(dest,scr,4)时很简单,但将自身的字符串复制到自身的下一个时就需要好好想一想,即实现char*=(char*)memcpy(scr+1,scr,4)</span>
相关文章
相关标签/搜索