strcpy的实现疑惑
关于strcpy的实现,有人说:
/* the emplementation in VC++ */
char* strcpy(char* dest, const char* src)
{
char* tmp = dest;
while (*tmp++ = *src++)
;
return dest;
}
/* the emplementation in Linux */
char* strcpy(char* dest, const char* src)
{
char* tmp = dest;
while ((*tmp++ = *src++) != '\0')
;
return dest;
}
同时,也有人说:
Linux 下的定义是这样的:
/usr/lib/string.h
string.h:
char *strcpy (char *__restrict __dest, __const char *__restrict __src)
__THROW;
/usr/src/linux-2.6.0-test3/lib/string.c
/**
* strcpy - Copy a %NUL terminated string
* @dest: Where to copy the string to
* @src: Where to copy the string from
*/
char * strcpy(char * dest,const char *src)
{
char *tmp = dest;
while ((*dest++ = *src++) != '\0')
/* nothing */;
return tmp;
}
还有:
char *strcpy(char *strDest,const char *strSrc)
{
assert((strDest!=NULL)&&(strSrc !=NULL))
char *address = strDest;
while((*strDest++ = *strSrc)!='\0')
NULL;
return address;
}这个好像是什么高质量C++什么的
请问,到底哪个才是正确的呢,是返回临时定义的指针还是返回参数中的destination str,为什么?
还有到底需要不要判断source和destination是不是NULL,不需要的理由是底层函数没必要进行这种判断
如果判断是用NULL还是用NULL,据说Bjarne Stroustrup本人是不赞成用NULL的。