1
votes

I want to compare two strings:

The first string is declared above my main:

char _newState[] = "AVAILABLE";

When I want to compare with a const string, i put the line:

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!

2

2 Answers

6
votes

It looks like you have your parameters in the wrong order. According to the C18 library manual the signature for strcmppgm2ram is

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.

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.

2
votes

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.