|
库函数strcpy的工作方式,如果编写一个标准strcpy函数的总分值为10,下面给出几个不同得分的答案:
2分
void strcpy( char *strDest, char *strSrc )
{
while( (*strDest++ = * strSrc++) != ‘\0’ );
}
4分
void strcpy( char *strDest, const char *strSrc )
//将源字符串加const,表明其为输入参数,加2分
{
while( (*strDest++ = * strSrc++) != ‘\0’ );
}
7分
void strcpy(char *strDest, const char *strSrc)
{
//对源地址和目的地址加非0断言,加3分
assert( (strDest != NULL) && (strSrc != NULL) );
while( (*strDest++ = * strSrc++) != ‘\0’ );
}
10分
//为了实现链式操作,将目的地址返回,加3分!
char * strcpy( char *strDest, const char *strSrc )
{
assert( (strDest != NULL) && (strSrc != NULL) );
char *address = strDest;
while( (*strDest++ = * strSrc++) != ‘\0’ );
return address;
}
从2分到10分的几个答案我们可以清楚的看到,小小的strcpy竟然暗藏着这么多玄机,真不是盖的!需要多么扎实的基本功才能写一个完美的strcpy啊!
csdn众:
wanfustudio(雁南飞:雁无留踪之意,水无取影之心)
/***
*strncpy.c - copy at most n characters of string
*
* Copyright (c) 1985-1997, Microsoft Corporation. All rights reserved.
*
*Purpose:
* defines strncpy() - copy at most n characters of string
*
*******************************************************************************/
char * __cdecl strcpy (
char * dest,
const char * source,
)
{
char *start = dest;
while (*dest++ = *source++) /* copy string */
;
*dest++ = '\0';
return(start);
}
char * __cdecl strncpy (
char * dest,
const char * source,
size_t count
)
{
char *start = dest;
while (count && (*dest++ = *source++)) /* copy string */
count--;
if (count) /* pad out with zeroes */
while (--count)
*dest++ = '\0';
return(start);
}
triace_zhang(『雪梨码头』做个编译器好难)
char* mystrcpy(char* dest,const char* src)
{
if ( ( dest == NULL) || ( src == NULL ) )
throw "Error: Empty Pointer(s) ";
char* p = dest;
while ((*dest++ = *src++) != '\0');
return p;
}
guohui4321(<看看海吹吹风>)
char* mystrcpy(char* dest,const char* src)
{
if ( src == NULL )
return dest;
char* p = dest;
while ((*dest++ = *src++) != '\0');
return p;
}
zkkpkk(菜鸟爱GDI)
char* strcpy(char* dest,const char* source)
{
if(*source == '\0')
{
*dest = '\0';
return dest;
}
char* address = dest;
while((*dest++ = *source++) != '\0');
return address;
} |
|