这道c错在哪儿?

djjlove_2008 2009-09-07 02:42:11
/* Note:Your choice is C IDE */
#include "stdio.h"
char *strcpy(char *strDest,const char *strSrc)
{
if((strDest == NULL) || (strSrc == NULL))
return NULL;
char *strDestCopy = strDest;
while((*strDest++ = *strSrc++) != '\0');
return strDestCopy;
}
int getStrLen(const char *strSrc)
{
int len = 0;
while(*strSrc++)
len++;
return len;
}
void main()
{
char strSrc[] = "Hello World!";
char strDest[20];
int len = 0;
len = getStrLen(strcpy(strDest,strSrc));
printf("strDest:%s\n",strDest);
printf("Length of strDest:%d\n",len);
}
...全文
256 29 打赏 收藏 转发到动态 举报
写回复
用AI写文章
29 条回复
切换为时间正序
请发表友善的回复…
发表回复
liate1 2009-09-09
  • 打赏
  • 举报
回复
[Quote=引用 20 楼 linuxshadow 的回复:]
int getStrLen(const char *strSrc)
{
int len = 0;
while(*strSrc++)
  len++;
return len;
}
* strSrc 你定义成为了const,为什么还要while(*strSrc++) ,这好象是有问题
大家可以看看,试试,我在公司没环境试
[/Quote]
=============
20楼的哥们说对了,const char *strSrc代表指针值不能变,但是你下面又while(*strSrc++),所以错误。
linuxshadow 2009-09-08
  • 打赏
  • 举报
回复
22楼的,你等于没说
djjlove_2008 2009-09-08
  • 打赏
  • 举报
回复
谢谢楼上各位,,,这道题目没有错哦,是编译器的问题,,,要用最新的编译器才行,呵呵。。。
zjs_worm 2009-09-07
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 june232 的回复:]
你的文件是以.c结尾吗?
如果是的话,变量的声明应当放到最前面,这是C的规定。所以第一个函数里面strDestCopy的声明有问题
[/Quote]
6楼正解!!

char *strcpy(char *strDest,const char *strSrc)
{
char *strDestCopy = strDest; //这几句应该是这样的顺序

if((strDest == NULL) || (strSrc == NULL))
return NULL;


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



TomorrowIsHistory 2009-09-07
  • 打赏
  • 举报
回复
没问题,这是道经典考试问题!
fengyuwuhen1 2009-09-07
  • 打赏
  • 举报
回复
等结果
asimay 2009-09-07
  • 打赏
  • 举报
回复
都是const的问题,因为是c IDE。


char *strcpy(char *strDest,const char *strSrc)
{
if((strDest == NULL) || (strSrc == NULL))
return NULL;
char *strDestCopy = strDest;
while((*strDest++ = *strSrc++) != '\0');
return strDestCopy;
}
int getStrLen(const char *strSrc)
{
int len = 0;
while(*strSrc++)
len++;
return len;
}

jinwei1984 2009-09-07
  • 打赏
  • 举报
回复
楼上两位 乱说吧 const修饰的什么。。。

没看出有什么问题

#include "stdio.h" 不知道为什么要用双引号 虽然也没错
lbjfeng 2009-09-07
  • 打赏
  • 举报
回复
lz,如果函数里面禁止修改的参数都可以用const修饰,
还是我的ls啊,nb
linuxshadow 2009-09-07
  • 打赏
  • 举报
回复
int getStrLen(const char *strSrc)
{
int len = 0;
while(*strSrc++)
len++;
return len;
}
* strSrc 你定义成为了const,为什么还要while(*strSrc++) ,这好象是有问题
大家可以看看,试试,我在公司没环境试
rejoice914 2009-09-07
  • 打赏
  • 举报
回复
没问题!别瞎说了!
JaneThink 2009-09-07
  • 打赏
  • 举报
回复
应该就是4楼和8楼说的两个问题吧
羽盛 2009-09-07
  • 打赏
  • 举报
回复
应该是没错啊,
羽盛 2009-09-07
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 henanxiaozi20050501 的回复:]
char *strcpy(char *strDest,const char *strSrc)
{
if((strDest == NULL) || (strSrc == NULL))
  return NULL;
char *strDestCopy = strDest;
while((*strDest++ = *strSrc++) != '\0');
returnstrDestCopy;  //返回的是野指针
}

[/Quote]
strDestCopy 不是野指针, char *strDestCopy = strDest; 已经赋过值了。
  • 打赏
  • 举报
回复
不好意思,看错了。
  • 打赏
  • 举报
回复
char *strcpy(char *strDest,const char *strSrc)
{
if((strDest == NULL) || (strSrc == NULL))
return NULL;
char *strDestCopy = strDest;
while((*strDest++ = *strSrc++) != '\0');
return strDestCopy; //返回的是野指针
}
Victor_Dinho 2009-09-07
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 liyudefly 的回复:]
引用 4 楼 victor_dinho 的回复:
C/C++ codeint getStrLen(constchar*strSrc)
{if (NULL== strSrc)////////////return0;//加上这两句int len=0;while(*strSrc++)
        len++;return len;
}

如果NULL == strSrc,while(*strSrc++) 第一次就为假,return 的值为0。
[/Quote]

不是的,我特意试过了~~~(*strSrc)为假等价于(*strSrc=='\0')。
atom_09 2009-09-07
  • 打赏
  • 举报
回复
7楼 *strDest = '\0' 这句不用加
atom_09 2009-09-07
  • 打赏
  • 举报
回复
问题大大的。。
liyudefly 2009-09-07
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 victor_dinho 的回复:]
C/C++ codeint getStrLen(constchar*strSrc)
{if (NULL== strSrc)////////////return0;//加上这两句int len=0;while(*strSrc++)
len++;return len;
}
[/Quote]
如果NULL == strSrc,while(*strSrc++) 第一次就为假,return 的值为0。

[Quote=引用 7 楼 kakashi0309 的回复:]
C/C++ codechar*strcpy(char*strDest,constchar*strSrc)
{if((strDest== NULL)|| (strSrc== NULL))return NULL;char*strDestCopy= strDest;while((*strDest++=*strSrc++)!='\0');*strDest='\0';//加上这句return strDes¡­
[/Quote]
while退出的条件就是*strDest == '\0',在它后面再加个'\0'没有意义。

[Quote=引用 6 楼 june232 的回复:]
你的文件是以.c结尾吗?
如果是的话,变量的声明应当放到最前面,这是C的规定。所以第一个函数里面strDestCopy的声明有问题
[/Quote]
顶这个,题目要求/* Note:Your choice is C IDE */ ,所以声明必须在最前面。
加载更多回复(9)

69,369

社区成员

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

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