标签:strcpy 编程效率 内存泄漏 编译器 string
先给出几种实现方式:
方式一:
<span style="font-size:14px;">char* strcpy(char* des,const char* source) { char* r=des; while((*(des++)=*(source++))!='\0'); return r; } </span>
方式二:
<div class="para"><span style="font-size:14px;">char * strcpy(char * strDest,const char * strSrc)</span></div><div class="para"><span style="font-size:14px;">{</span></div><div class="para"><span style="font-size:14px;">if ((NULL==strDest) || (NULL==strSrc)) //[1]</span></div><div class="para"><span style="font-size:14px;">throw "Invalid argument(s)"; //[2]</span></div><div class="para"><span style="font-size:14px;">char * strDestCopy = strDest; //[3]</span></div><div class="para"><span style="font-size:14px;">while ((*strDest++=*strSrc++)!='\0'); //[4]</span></div><div class="para"><span style="font-size:14px;">return strDestCopy;</span></div><div class="para"><span style="font-size:14px;">} </span></div>
方式三:
<span style="font-size:14px;">char * strcpy(char * strDest, const char * strSrc) { if ((!strDest) || (!strSrc)) throw "Invalid argument(s)"; char * strDestCopy = strDest; while ((*strDest++ = *strSrc++) != '\0'); return strDestCopy; }</span>
方式四:
<span style="font-size:14px;">char * strcpy(char * strDest, const char * strSrc) { assert((NULL == strDest) || (NULL == strSrc)); char * strDestCopy = strDest; while ((*strDest++ = *strSrc++) != '\0'); return strDestCopy; }</span>
还有其他实现方式。。。。。。。。。。
(1)编程风格;
(2)出错处理;
(3)算法复杂度分析(用于提高性能)。
标签:strcpy 编程效率 内存泄漏 编译器 string
原文地址:http://blog.csdn.net/gggg_ggg/article/details/43450821