1,不调用C++/C的字符串库函数,请编写函数 strcpy。
/********************************************************************
created: 2012/09/06
created: 6:9:2012 21:17
filename: E:\C\strcpy\myStrcpy.c
file path: E:\C\strcpy
file base: myStrcpy
file ext: c
author:
purpose:
*********************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
char *myStrcpy( char *dst, const char *src );
int main()
{
char str[] = "String copy finished!";
char dst[128] = " ";
printf( "str = %s\n", str );
printf( "dst = %s\n", myStrcpy( dst, str ) );
return 0;
}
char *myStrcpy( char *dst, const char *src )
{
char *address = dst;
assert( ( dst != NULL ) && ( src != NULL ) );
/*
** copy until *src == '\0'
*/
while ( (*dst++ = *src++) != '\0' )
;
return address;
}
2,strcpy能把strSrc的内容复制到strDest,为什么还要char * 类型的返回值?
为了实现链式表达式。例如 int length = strlen(strcpy( strDest, “hello world”) );