zj@zj:~/C_parm/string/own_str/strcpy$ cat strcpy.c
/*
下面是strcpy库函数的实现,因为库函数讲究的就是精练、简洁。所以没有其他的异常处理代码。主要的异常处理还是交给了函数的使用者,在调用前请确认目的和源指针是否都存在(不能为Null),请确认目标指针空间是否大于源字符串的空间。
Copies the string src into the spot specified by dest; assumes enough room.
目标指针空间必须大于源字符串空间。
*/
#include<stdio.h>
#include<stdlib.h>
#define MAX_LEN 255
char* my_strcpy(char* dst, const char* src);
int main()
{
char a[MAX_LEN];
char* str = "Hello,world!";
printf("Copied:%s\n",my_strcpy(a,str));
exit(EXIT_SUCCESS);
}
char* my_strcpy(char* dst, const char* src)
{
if((dst==NULL)||(src==NULL))
perror("dst or src is null");
char* cp = dst;
while((*cp++=*src++)!='\0')
;
return (dst);
}
zj@zj:~/C_parm/string/own_str/strcpy$ ./strcpy
Copied:Hello,world!
|