20170912_字符串拷贝strcpy的实现
//strcpy(des,src)实现了把从src地址开始的且含有'\0'结束符的字符串复制到
//以des开始的地址空间内。
#include<iostream>
#include<cstdio>
#include<cassert>
using namespace std;
char *strCpy(char *des, const char *src) //src参数不容改变,des要能改变,同时des空间要足够大!
{ //假设不存在des长度小于src长度的情况,不然会出错!
assert(des!=NULL && src!=NULL);
char *address=des;
while((*des=*src)!='\0')
{
++des;
++src;
}
if((des+1)!=NULL) //src长度小于des的长度,那么原来的des后面的字符也要清空!
{
++des;
while((*des)!='\0')
*(des++)=NULL;
}
return address; //要有返回值
}
int main()
{
//char src[]="abcdef";
/*char src[]="";*/
char src[]="abcde";
char des[100]="ABCDEFGHI";
cout<<"Input string is: "<<des<<","<<src<<endl;
char *res;
res=strCpy(des,src);
cout<<"strcpy result is: "<<res<<endl<<endl;
cout<<(*(res+6))<<" "<<(*(res+10))<<" "<<sizeof(des)<<endl;
system("pause");
return 0;
}