简体   繁体   English

C18 Microchip编译器和strcmp函数

[英]C18 Microchip compiler and strcmp function

I want to compare two strings: 我想比较两个字符串:

The first string is declared above my main: 第一个字符串在我的main上方声明:

char _newState[] = "AVAILABLE";

When I want to compare with a const string, i put the line: 当我想与const字符串进行比较时,我将这一行:

if(strcmppgm2ram((const char *) "AVAILABLE", _newState ) == 0){
    code:
}

The function never returns a zero, what is the solution and the right typecast? 该函数永远不会返回零,那么解决方案和正确的类型转换是什么? strcmp is the same problem! strcmp是同样的问题!

It looks like you have your parameters in the wrong order. 看起来您的参数顺序错误。 According to the C18 library manual the signature for strcmppgm2ram is 根据C18库手册, strcmppgm2ram的签名为

signed char strcmppgm2ram(const char * str1, const rom char * str2 );

So your strng constant should be the second string and your character array should be the first parameter. 因此,您的strng常量应为第二个字符串,而字符数组应为第一个参数。

You should not use casts as all they do is hide issues like this. 您不应该使用演员表,因为它们所做的只是隐藏这样的问题。 If you have a type mismatch then you should use that information to determine what the correct type should be and whether you have made a mistake. 如果类型不匹配,则应该使用该信息来确定正确的类型以及是否犯了错误。 Using a cast is like telling the compiler to ignore what you've done even if the compiler thinks it should be a warning/error. 使用强制转换就像告诉编译器忽略您所做的一样,即使编译器认为它应该是警告/错误。

Try: 尝试:

const far rom char _newState[] = "AVAILABLE";

For future reference it's a good idea not to ignore compiler warnings - they are there to help you. 为了将来参考,最好不要忽略编译器警告-它们可以为您提供帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM