C language simulation to achieve strcpy function

For C language simulation to implement strcpy:
1. Know that the function of strcpy is: copy the string starting from the source address and containing'\0' to the address space of the target
2. Pay attention to the design of parameters, the design of return value types, assert The use of the parameter part of the use
of const Example:
header file

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

my_strcpy function part

char* my_strcpy(char* dest, const char* src)//用char*是为了实现链式访问
{
	char* cp = dest;
	assert(src != NULL);//断言
	assert(dest != NULL);
	while(*cp++ = *src++)
	{
		;
	}
	return dest;	
}

Main function

int main()
{
	char arr[10] = "*********";
	char *str = "abc12345";
	my_strcpy(arr, str);
	printf("%s\n", arr);
	system("pause");
	return 0;
}

Guess you like

Origin blog.csdn.net/Beer_xiaocai/article/details/84350783