找回密码
 注册
查看: 548|回复: 5

帮忙看一下一个字符数组的例题,谢谢!

[复制链接]
发表于 2005-4-20 09:48:22 | 显示全部楼层 |阅读模式
代码

#include "stdio.h"
main()
{
char string[81];
int i,num=0,word=0;
char c;
gets (string);
for (i=0;(c=string)!="\0";i++);
   if (c==" ") word=0;
   else if (word==0)
         {
          word=1;
          num++;
         }
   printf("There are %d words in the line\n",num);
}

编译我用的 gcc -o 6.8.out 6.8.c
提示:
6.8.c: In function `main':
6.8.c:8: warning: comparison between pointer and integer
6.8.c:9: warning: comparison between pointer and integer
/tmp/ccMve2Ss.o(.text+0x32): In function `main':
: warning: the `gets' function is dangerous and should not be used


我是按照例题打下来的,难道还有错吗?
发表于 2005-4-20 10:02:02 | 显示全部楼层
是警告,不是error,程序应该编译通过了的.在指针与整数之间比较当然会警告你了,看来楼主的c不熟啊
回复

使用道具 举报

 楼主| 发表于 2005-4-20 12:15:59 | 显示全部楼层
4242
刚学嘛,可是这个是 谭浩强 的例题嘛,我以为没有什么问题...

请问,我应该如何改一下?
回复

使用道具 举报

 楼主| 发表于 2005-4-20 12:50:59 | 显示全部楼层
我运行后,输入 i am a boy
提示 Segmentation fault

怎么回事?
哪里能找到这种错误代码所代表的错误类型...
回复

使用道具 举报

发表于 2005-4-20 13:06:38 | 显示全部楼层

少个在括号吧?

FOR语句后面怎么没有语句体,少了个大括号把后面的if else语句加入吧!!
回复

使用道具 举报

发表于 2005-4-20 21:00:57 | 显示全部楼层
#include "stdio.h"
main()
{
char string[81];
int i,num=0,word=0;
char c;
gets (string);
for (i=0;(c=string)!="\0";i++){
if (c==" ") word=0;
else if (word==0)
{
word=1;
num++;
}
}
printf("There are %d words in the line\n",num);
}
改成上面那样,经我验证,正常
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

GMT+8, 2025-2-8 20:42 , Processed in 0.062026 second(s), 16 queries .

© 2001-2025 Discuz! Team. Powered by Discuz! X3.5.

快速回复 返回顶部 返回列表