使用strcmp结果false比较两个数组但是两个数组都包含相同的单词 [英] comparing two array using strcmp result false however both array contain same word

查看:65
本文介绍了使用strcmp结果false比较两个数组但是两个数组都包含相同的单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 / *这里显示的nog和arrr显示单词ras但是在比较时返回false结果为什么??? * / 

Void ldata(unsigned char [])
{
//在lcd上打印数据的代码
}

Char nog [] =ras;
Void rad(char arrr [])
{
For(g = 0; g< strlen(arrr); g ++)
{
Ldata(arrr [g ]);
}
For(g = 0; g< strlen(nog); g ++)
{
Ldata(nog [g]);
}
如果(strcmp(arrr,nog)== 0)
{
Led1 = 1;
}
}

解决方案

如果它们包含完全相同的字符串,则strcmp返回0 - 所以如果你的Led1没有得到1然后他们显然没有相同的值。 LCD显示器可能没有显示 - nog 数组将有四个字符'r','a','s'和一个空终止符 - 但我们不知道 arrr 包含什么。它可以像声明一样简单

  char  aaar [] =   ras; 

这将导致strcmp失败,但不会在显示器上显示任何奇怪的内容。您需要更仔细地查看实际值 - 可能是通过显示十六进制值而不是字符本身?



下次在此处发布代码时,请确保它正是你正在执行的 - 我知道不是,因为c不会编译,因为c区分大小写,而如果与<$ c $不同c>如果,的不同,依此类推。


/* here nog and arrr on displaying shows the word ras but on compare return false result why???*/

Void ldata(unsigned char[])
{
// code for printing data on lcd
}

Char nog[]="ras";
Void rad (char arrr[])
{
For(g=0;g<strlen(arrr);g++)
{
Ldata(arrr[g]);
}
  For(g=0;g<strlen(nog);g++)
{
Ldata(nog[g]);
}
If(strcmp(arrr,nog)==0)
{
Led1=1;
}
}

解决方案

If they contain exactly the same string, then strcmp returns 0 - so if your Led1 isn't getting a 1 then they clearly don't have the same value. A LCD display may not show that - the nog array will have four characters 'r', 'a', 's', and a null terminator - but we don't know what arrr contains. It could be as simple as the declaration

char aaar[] = "ras ";

which would cause strcmp to fail, but wouldn't show anything odd on your display. You need to look more closely ate the actual values - perhaps by displaying the hex values instead of the characters themselves?

And next time you post code here, make sure it is exactly what you are executing - I know that isn't, because that won't compile as c is case sensitive, and if is not the same as If, for is not the same as For, and so on.


这篇关于使用strcmp结果false比较两个数组但是两个数组都包含相同的单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆