C语言中strcpy是一个标准库函数,其作用是把含有'\0'结束符的字符串复制到另一个地址空间中。
#include <stdio.h>
#include "string.h"
#include <assert.h>
#define _CRT_SECURE_NO_WARNINGS
char* my_strcpy(char* a, const char* b)
{
assert(a != NULL && b != NULL);
char* pdest = a;
const char* psrc = b;
while (*psrc != '\0')
{
*pdest++ = *psrc++;
}
*pdest = '\0'; //以'\0'结束
return a;
}
int main()
{
char str1[20] = "abcdef";
char *str2 = "hello";
printf("str1=%s\n", str1);
my_strcpy(str1,str2);
printf("str1=%s\n", str1);
}