转载-strcpy 为什么需要返回值 为char*

有时候函数原本不需要返回值,但为了增加灵活性如支持链式表达,
可以附加返回值。
例如字符串拷贝函数strcpy 的原型:
char *strcpy(char *strDest,const char *strSrc);
strcpy 函数将strSrc 拷贝至输出参数strDest 中,同时函数的返回值又是strDest。
这样做并非多此一举,可以获得如下灵活性:
char str[20];

int length = strlen( strcpy(str, “Hello World”) );


  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. char* Strcpy(char *x ,const char*y) //把y 拷贝到 x 中去
  4. {
  5. int i = 0 ;
  6. while((x[i]= y[i]) != '\0')
  7. {
  8. i++ ;
  9. }
  10. return x;
  11. }
  12. int main()
  13. {
  14. char *a = "hello world" ;
  15. char b[ 10];
  16. Strcpy(b ,a);
  17. puts(b);
  18. }

猜你喜欢

转载自blog.csdn.net/u012308586/article/details/81014280
今日推荐