strcpy返回值问题

wibnmo 2010-05-29 01:03:36
同样的一段代码在vc++6.0和linux gcc编译后的结果是不一样的:
vc++6.0:
hello world
11
Press any key to continue
linux gcc:
hello world
3

#include <stdio.h>
#include <string.h>

char *strcpy2(char *strDest, const char *strSrc)
{

char *address = strDest;
while ((*address++ = *strSrc++) != '\0');
return address;
}

int main(void)
{
char strDest[20];
int length = strlen(strcpy2(strDest, "hello world"));
printf("%s\n",strDest);
printf("%d\n",length);

return 0;
}
...全文
276 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
wibnmo 2010-05-30
  • 打赏
  • 举报
回复

题都看错了。。丢人丢大了。。赶快结帖。。囧囧囧囧囧囧囧囧
pengzhixi 2010-05-29
  • 打赏
  • 举报
回复
你再好好把那帖子看看吧
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 wibnmo 的回复:]

出现频率最高的笔试题strcpy写法+分析zz
http://topic.csdn.net/u/20090901/08/1a5757e5-3467-4c27-884b-dd7401888dd4.html?15827
精华帖里也是这么写的。
为什么address走到最后了,返回它的值之后进行strlen运算,结果还是11?
[/Quote]


楼主请看清楚while循环中是用的address??
他用的是strDest!!
东莞某某某 2010-05-29
  • 打赏
  • 举报
回复
返回的指针指向str1+strlen("hello world")
yoan_T 2010-05-29
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 wibnmo 的回复:]
出现频率最高的笔试题strcpy写法+分析zz
http://topic.csdn.net/u/20090901/08/1a5757e5-3467-4c27-884b-dd7401888dd4.html?15827
精华帖里也是这么写的。
为什么address走到最后了,返回它的值之后进行strlen运算,结果还是11?
[/Quote]
贴中做操作的指针和你的都不一样当然返回的也不一样
建鼎呓语 2010-05-29
  • 打赏
  • 举报
回复
未知内存区域,什么结果都是可能的~
建鼎呓语 2010-05-29
  • 打赏
  • 举报
回复
回楼上,碰巧~
wibnmo 2010-05-29
  • 打赏
  • 举报
回复
出现频率最高的笔试题strcpy写法+分析zz
http://topic.csdn.net/u/20090901/08/1a5757e5-3467-4c27-884b-dd7401888dd4.html?15827
精华帖里也是这么写的。
为什么address走到最后了,返回它的值之后进行strlen运算,结果还是11?
fairuyy 2010-05-29
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 pengzhixi 的回复:]
return strDest;
[/Quote]


对头,因为address 已经走到字符串的最后.
pengzhixi 2010-05-29
  • 打赏
  • 举报
回复
return strDest;

69,364

社区成员

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

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