strcpy是将一个拷贝字符串的函数。
下面我们进行strcpy的模拟实现:
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<assert.h>
char* my_strcpy(char* dest,const char* str)
//char*作为返回值是方便链式访问
//const是为防止str被改变
{
char* ret = dest;
assert(dest != NULL);
//断言
assert(str != NULL);
while (*(dest++) = *(str++))
{
;
}
return ret;
}
int main()
{
char arr[20] = { 0 };
my_strcpy(arr, "abcdefg");
printf("%s\n", arr);
printf("%s\n", my_strcpy(arr, "abcdefg"));
system("pause");
return 0;
}
输出结果为: