码迷,mamicode.com
首页 > 编程语言 > 详细

C语言实现strcpy

时间:2014-09-08 10:47:56      阅读:251      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   color   io   ar   strong   div   sp   

strcpy.h:

 

 1 #ifndef STRCPY_H
 2 #define STRCPY_H
 3 
 4 #include <stdio.h>
 5 
 6 char *cat_strcpy(char *dst, const char *src) {
 7     if (NULL == src || NULL == src)
 8         return NULL;
 9 
10     char *s = (char *)src, *d = dst;
11 
12     while ((*d++ = *s++) != \0) {
13         // do nothing
14     }
15     *d = \0;
16 
17     return dst;
18 }
19 
20 #endif

 

 

main:

 1
 2 #include "strcpy.h"
 3 
 4 
 5 void test_strcpy();
 6 
 7 int main() {
 8     test_strcpy();
 9 
10     return 0;
11 }
12 
13 
14 void test_strcpy() {
15     char *src = "test_strcpy";
16     char dest[20] = { 0 };
17     cat_strcpy(dest, src);
18 
19     printf("%s\n", dest);
20 }

 

strcpy(str1,str2)函数能够将str2中的内容复制到str1中,为什么还需要函数返回值应该是方便实现链式表达式,比如:

int length = strlen(strcpy(str1,str2));

 

 

 

C语言实现strcpy

标签:des   style   blog   color   io   ar   strong   div   sp   

原文地址:http://www.cnblogs.com/lingshaohu/p/3961132.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!