C库函数memcpy的代码

 

一、内存拷贝函数

void memcpy(void* pvTo, void* pvFrom, size_t size)

{

void* pbTo = (byte*)pvTo;

void* pbFrom = (byte*)pvFrom;

assert(pvTo != NULL && pvFrom != NULL);

while(size-->0)

*pbTo++ == *pbFrom++;

return(pvTo);

}

 

二、字符串拷贝函数

1)不调用C++/C的字符串库函数,请编写函数 strcpy

char *strcpy(char *strDest, const char *strSrc);

{

    assert((strDest!=NULL) && (strSrc !=NULL));// 2

    char *address = strDest;// 2

    while( (*strDest++ = * strSrc++) != \0 )// 2

       NULL ;

    return address ;           // 2

}

 

2strcpy能把strSrc的内容复制到strDest,为什么还要char * 类型的返回值?

答:为了实现链式表达式。// 2

例如int length = strlen( strcpy( strDest, hello world) );

 

char* strcpychar*strDestconst char*strSrc

{

assert(strDest!=NULL&&strSrc!=NULL);    //2

char* address=strDest;               //2

while(*(strDest++)=*(strSrc++)!='\0') //2

  NULL;

return address;                  //2

}

posted on 2007-09-11 11:51  highmayor  阅读(1146)  评论(0编辑  收藏  举报

导航