strcmp() 不灵光了?

seaboat_wang 2010-03-01 07:46:05

#inculde <stdio.h>
#inculde <string.h>

int main(void)
{
int i;
int status;
int nbytes = 20;
int equal = 0;

/*Initialize instructions empty string*/
for(i=0;i<100;i++)
instructions[i] = "";

/*Prompt user to input instructions*/
i = 0;
while( i<100)
{
printf("%s","Please input next instruction:\n");
fflush(stdout);
instructions[i] = (char *) malloc (nbytes + 1);
status = getline (&instructions[i], &nbytes, stdin);
if (status == -1)
{
puts ("ERROR!");
}
else
{
puts ("You typed:");
puts (instructions[i]);
}
equal = strcmp("HALT",instructions[i]);
printf("Value in instructions is: %s\n",instructions[i]);
printf("Equal's value is: %d\n",equal);
i++;
}
}



用gcc编译
运行以后, 输入 HALT
程序给的输出是 -1
有点儿不明白为什么
然后有写了一个简单的程序试了试strcmp()

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

int main(void)
{
char *s = "HELLO";
printf("%d\n",strcmp("HELLO",s));
return 0;
}


这样的话, 结果又是对的。
(程序输出结果是0)


那么说明肯定是我的程序错了。
小弟不才, 一时没有找出自己的错误
望高人指点!



感谢! n_n
...全文
261 20 打赏 收藏 转发到动态 举报
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
seaboat_wang 2010-03-03
  • 打赏
  • 举报
回复
感谢各位, 学习了!

小结自己的错误:
1. 申请了内存没有及时释放;
2. 没有弄清楚函数的使用方法;



再次感谢!
n_n
Happylessness 2010-03-02
  • 打赏
  • 举报
回复
修改如下内容后,再试一下:
初始化 malloc后的内存。
或者getline以后,把串的最后一位加一个"/0"。


另外,要记得 delete和malloc成对儿使用,否则会内存泄露。
菜鸟飘过。。。
angel_su 2010-03-02
  • 打赏
  • 举报
回复
引用 17 楼 smallbear_2008 的回复:
打印也是一种手段...

\n打印不容易察看出来吧,应该学一下如何用ide里的debug功能,设个断点观察一下变量值就知道毛病...
bladesoft 2010-03-01
  • 打赏
  • 举报
回复
strcmp没有问题,而是你的instructions[i]最后有个/n,所以输入HALT,比较后为-1.
另外楼主代码好像没有拷贝全,include拼写错误,instructions没有声明,malloc的内存也没有free,是很危险的。

7楼代码是对的,不过gcc不支持get,建议用fget,不然会出warning.
wesleyluo 2010-03-01
  • 打赏
  • 举报
回复
先顶下帖,等下测试下。
nobody@noone 2010-03-01
  • 打赏
  • 举报
回复
你应该跟进去看instructions[i]这个到底是什么
tang056 2010-03-01
  • 打赏
  • 举报
回复
希望各位可以给出调试完的代码,不要只说一两句话而已
smallbear_2008 2010-03-01
  • 打赏
  • 举报
回复
引用 14 楼 seaboat_wang 的回复:
引用 10 楼 azraeln 的回复:<br /> 你应该跟进去看instructions[i]这个到底是什么 <br />


呃。。。这个“跟进去”是怎么个跟发呀?

我只知道printf instruction[i]里面的内容。。。。。


打印也是一种手段。
现在equal=-1,那么它的上一句打印中的末尾能看出有 空格吗?%s是遇到\0才结束的
smallbear_2008 2010-03-01
  • 打赏
  • 举报
回复
引用 12 楼 bladesoft 的回复:
strcmp没有问题,而是你的instructions[i]最后有个/n,所以输入HALT,比较后为-1.
另外楼主代码好像没有拷贝全,include拼写错误,instructions没有声明,malloc的内存也没有free,是很危险的。

7楼代码是对的,不过gcc不支持get,建议用fget,不然会出warning.


malloc 的返回值也是要检查的。
cattycat 2010-03-01
  • 打赏
  • 举报
回复
对getline中包含\n的。
tang056 2010-03-01
  • 打赏
  • 举报
回复
一下代码完全可以实现楼主的功能,请楼主自行对照代码找出不同之处,
相信楼主可以得到自己需要的答案。。。
呵呵,不要忘了给点分哦,呵呵!!!

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

#define NBYTES 20
#define NBYTE_Number 5

int main(void)
{
int i;
int status=1;//便于在C环境中程序的运行,始终保持为有效状态
char *instructions[NBYTES];
int equal = 0;

i = 0;
printf("Please input the first instruction:\n");
while(i<NBYTE_Number)
{
fflush(stdout);
instructions[i] = (char *) malloc (NBYTES + 1);
memset(instructions[i], 0x00, NBYTES + 1);
if (status == -1)
{
puts ("ERROR!");
}
else
{
puts ("You typed:");
if(i!=0)
printf("Please input next instruction:\n");
gets (instructions[i]);
}
equal = strcmp("HALT",instructions[i]);
printf("Value in instructions is: %s\n",instructions[i]);
free(instructions[i]);
printf("Equal's value is: %d\n",equal);
i++;
}

return 0;
}
seaboat_wang 2010-03-01
  • 打赏
  • 举报
回复
引用 12 楼 bladesoft 的回复:
strcmp没有问题,而是你的instructions[i]最后有个/n,所以输入HALT,比较后为-1.
另外楼主代码好像没有拷贝全,include拼写错误,instructions没有声明,malloc的内存也没有free,是很危险的。

7楼代码是对的,不过gcc不支持get,建议用fget,不然会出warning.


请问那C里面有没有像 Perl里面 chomp()这样的命令呢? :p
seaboat_wang 2010-03-01
  • 打赏
  • 举报
回复
引用 10 楼 azraeln 的回复:
你应该跟进去看instructions[i]这个到底是什么



呃。。。这个“跟进去”是怎么个跟发呀?

我只知道printf instruction[i]里面的内容。。。。。
vrace 2010-03-01
  • 打赏
  • 举报
回复
getline 拿的东西包含回车的
seaboat_wang 2010-03-01
  • 打赏
  • 举报
回复
引用 3 楼 haozi8993 的回复:
instructions[i] = (char *) malloc (nbytes + 1);
后面要加memset(instructions[i],0,nbytes + 1);
你原来内存有垃圾数据,当然不等了。


请问“内存有垃圾”是怎么回事儿啊?
能讲解一下么?

谢谢啦!
sk19891117 2010-03-01
  • 打赏
  • 举报
回复
输入的HALT后面还有'\n'哦
楼主的fflush(stdout); 应该是fflush(stdin);吧
赵4老师 2010-03-01
  • 打赏
  • 举报
回复
输入的HALT后面可能还有'\n'
罗耗子 2010-03-01
  • 打赏
  • 举报
回复
instructions[i] = (char *) malloc (nbytes + 1);
后面要加memset(instructions[i],0,nbytes + 1);
你原来内存有垃圾数据,当然不等了。
CNnumen 2010-03-01
  • 打赏
  • 举报
回复
instructions[i] = (char *) malloc (nbytes + 1); 这句话不应该放在while(){}循环中吧...

#define NBYTES 100

char* instructions = (char*)malloc(NBYTES + 1);
memset(instructions, 0x00, NBYTES + 1);

while(i<100)
{
....
}


yitaohust 2010-03-01
  • 打赏
  • 举报
回复
貌似include就写错了,咋编译过了?

69,373

社区成员

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

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