strcpy的实现

来源:互联网 发布:淘宝下载安装最新版 编辑:程序博客网 时间:2024/04/30 11:17

在前面的文章中,我们详细介绍了memcpy函数的实现及其缺陷,本文我们主要介绍下strcpy函数的实现及其与memcpy函数的区别

一样,通过google搜搜strcpy,我们可以得到以下一段话

<cstring>

char * strcpy ( char* destination, const char * source );

Copy string

Copies the C string pointed by source into the arraypointed by destination,including theterminating null character (and stopping at that point).

To avoid overflows, the size of the array pointed by destination shallbe long enough to contain the same C string as source (includingthe terminating null character), andshould not overlapin memory with source.

从以上一段话中,我们可以得到以下几条信息

1、strcpy函数声明变了,不在是void *类型而是char *类型了char * strcpy ( char* destination, const char * source );

2、strcpy不需要指定拷贝字节数,而memcpy需要指定

3、strcpy遇到null字符就结束,而memcpy需要拷贝固定的num个字节,即使中间遇到null也会拷贝

4、和memcpy一样只能进行前向拷贝,而不能进行后向拷贝


strcpy的实现如下所示

char *strcpy(char *dest, const char *src){char *tmp = dest;while ((*dest++ = *src++) != '\0');return tmp;}

原创粉丝点击