老面试题:strcpy函数为什么要返回char*?(刚攒起来的50分送您)

kangxidadi 2005-01-09 04:50:50
谢谢答复!
...全文
2127 8 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
myorange520 2005-01-09
  • 打赏
  • 举报
回复
若你有vc6.0的msdn的话,你可以查一下该函数说明:
以下是msdn 该函数 的部分说明:

char *strcpy( char *strDestination, const char *strSource );

Return Value

Each of these functions returns the destination string. No return value is reserved to indicate an error.

Parameters

strDestination

Destination string

strSource

Null-terminated source string

Remarks

The strcpy function copies strSource, including the terminating null character, to the location specified by strDestination. No overflow checking is performed when strings are copied or appended. The behavior of strcpy is undefined if the source and destination strings overlap.

它的返回值除了可以返回strDestination的指针外,也可以返回错误信息。当该函数发生错误时,strDestination = strcpy();会破坏strDestination 。 所以最好不要使用strDestination = strcpy();
这种风格。



YFY 2005-01-09
  • 打赏
  • 举报
回复

前半部分也给你:

(1)不调用C++/C的字符串库函数,请编写函数 strcpy
char *strcpy(char *strDest, const char *strSrc);
{
assert((strDest!=NULL) && (strSrc !=NULL)); // 2分
char *address = strDest; // 2分
while( (*strDest++ = * strSrc++) != ‘\0’ ) // 2分
NULL ;
return address ; // 2分
}
Roaming_Sheep 2005-01-09
  • 打赏
  • 举报
回复
哦,怪我没讲明白,char *strcpy(char *dst, const char *src);这么一个c运行时函数,要拷贝直接调用strcpy(a,b)就可以把b字串内容拷贝给a指针指向的内存块,并不用a = strcopy(a,b),为什么还要返回目标的指针呢?
——————————————————
但是可以这样:
strcpy(c, strcpy(b, a));
YFY 2005-01-09
  • 打赏
  • 举报
回复

林锐的高质量C/C++编程里回答:

2)strcpy能把strSrc的内容复制到strDest,为什么还要char * 类型的返回值?
答:为了实现链式表达式。 // 2分
例如 int length = strlen( strcpy( strDest, “hello world”) );

^_^...
kangxidadi 2005-01-09
  • 打赏
  • 举报
回复
哦,怪我没讲明白,char *strcpy(char *dst, const char *src);这么一个c运行时函数,要拷贝直接调用strcpy(a,b)就可以把b字串内容拷贝给a指针指向的内存块,并不用a = strcopy(a,b),为什么还要返回目标的指针呢?
fatalerror99 2005-01-09
  • 打赏
  • 举报
回复
C/C++ 语言的参数传递和返回值都不可能是数组,因为在 C/C++ 中数组的内部表示就是指针,数组本身没有长度信息,不可能作为参数或返回值。
learn2003 2005-01-09
  • 打赏
  • 举报
回复
数组
fatalerror99 2005-01-09
  • 打赏
  • 举报
回复
如果不返回 char*,还能返回什么呢?你想让它返回什么?

70,028

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧