首页 > 技术文章 > 实现拷贝函数(strcpy)

nothx 2018-03-05 20:11 原文

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 
 4 // 函数声明
 5 char *mystrcpy(char *object, char *source);
 6 
 7 void main()
 8 {
 9     char str[100];
10     char *p = "Hello,15PB";
11 
12     printf("%s", mystrcpy(str, p));
13 
14     system("pause");
15 }
16 
17 // 拷贝函数
18 char *mystrcpy(char *object, char *source)
19 {
20     if (object == NULL || source == NULL)
21     {
22         return NULL;
23     }
24 
25     char *pstr = object;
26 
27     while (*source != '\0')
28     {
29         *object = *source;
30         object++;
31         source++;
32     }
33     *object = *source;    // 处理'\0'
34 
35 //     while ((*object++ = *source++))
36 //     {
37 // 
38 //     }
39     return pstr;
40 }

 

推荐阅读