strcpy
来源:互联网 发布:淘宝宝贝图片拍摄技巧 编辑:程序博客网 时间:2024/06/17 13:54
#include <assert.h>#include <iostream> using namespace std;char * strcpy(char * strDest,const char *strSrc){assert((strDest!=NULL) && (strSrc !=NULL));//1char * strDestCopy=strDest;//2while ((*strDest++=*strSrc++)!='\0');//3 \\将strsrc的\0复制给strdest然后判断 然后指针再往后移return strDestCopy;//4}void main(){char a[20],c[]="i am teacher!";strcpy(a,c);cout<<a<<endl;}
strcpy能把strSrc的内容复制到strDest,为什么还要char * 类型的返回值?
答:为了实现链式表达式。
例如 int length = strlen( strcpy( strDest, “hello world”) );
--------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------
就是方便一些,否则就要这样写:
char strDest[12];
strcpy( strDest, “hello world”);
int length = strlen(strDest);
--------------------------------------------------------------------------------------------------------------------------------
其实,说白了,就是如果上面的字符串拷贝函数strcopy的返回值是void,那么,上面那句:
int length = strlen( strcpy( strDest, “hello world”) );
就要像上面那位的回答,写成好几句了:
char strDest[12];
strcpy( strDest, “hello world”);
int length = strlen(strDest);
而这种直接返回char *的手段,就是为了后来函数调用者方便而设计的.不用你这么麻烦用上述方法去使用了,而直接可以使用拷贝后的dest字符串了.这种方便的实现方法,看起来就是链子链在一起的,所以称为 链式表达式
如此而已.呵呵.
- strcpy
- strcpy
- strcpy
- strcpy
- strcpy
- strcpy
- strcpy
- strcpy
- strcpy
- strcpy
- strcpy
- strcpy
- strcpy
- strcpy
- strcpy
- strcpy
- strcpy
- strcpy
- Hustoj (poj2104)线段树(划分树)
- erlang 实现list的二分查找
- 数据结构 第二章 线性表 思维导图
- Codeforces Round #271 (Div. 2) B
- struts2.x加入百度ueditor在线编辑框
- strcpy
- iOS常用设计模式——原型模式
- UVA10010
- RTC没有工作的原因分析
- 常规php设置
- vs2010旗舰中文版MSDN的安装
- Mysql NDB 常见问题
- 基本算法实现
- Unity3D的四种坐标系