天天看点

基本语言细节--C语言标准库函数strcpy

基本语言细节--C语言标准库函数strcpy 1.介绍 原型声明:extern char *strcpy(char* dest, const char *src); 头文件:#include <string.h> 功能:把从src地址开始且含有NULL结束符的字符串复制到以dest开始的地址空间 说明:src和dest所指内存区域不可以重叠且dest必须有足够的空间来容纳src的字符串。 返回指向dest的指针。 2.实现:   char * strcpy ( char *d, const char *s)  {    char *r=d;    while ((*d++=*s++));    return r;  }   3.自己动手写的一段strcpy函数:

char * strcpy(char *pstr_destination,const char * pstr_source)
{
      if((nullptr!=pstr_destination) && (nullptr!=pstr_source))
      {
             char *pstr_destination_copy=pstr_destination;
             while((*pstr_destination++=*pstr_source++)!=‘/0’) ;
             return pstr_destination_copy;
       }
       else
       { 
              return nullptr; 
       }
}
           

4.注意:

返回

pstr_destination
           

的原始值使函数能够支持链式表达式,增加了函数的“附加值”。同样功能的函数,如果能合理地提高的可用性,自然就更加理想。 链式表达式的形式如: int iLength=strlen(strcpy(strA,strB)); 若是不返回开始的字符串地址,则链式表达式失效!

继续阅读