简体   繁体   中英

How do I check if a value matches a string

I have a struct here with something like:

char *sname;
........
players[i].sname

equalling "James".

I need to check for equality between values like so:

if (players[i].sname == 'Lee')

but am not having much luck. Is there a str* function I should be using or is there anyway to fix up my if statement?

The short answer: strcmp() .

The long answer: So you've got this:

if(players[i].sname == 'Lee')

This is wrong in several respects. First, single-quotes mean "character literal" not "string literal" in C.

Secondly, and more importantly, "string1" == "string2" doesn't compare strings, it compares char * s, or pointers to characters. It will tell you if two strings are stored in the same memory location . That would mean they're equal, but a false result wouldn't mean they're inequal .

strcmp() will basically go through and compare each character in the strings, stopping at the first character that isn't equal, and returning the difference between the two characters (which is why you have to say strcmp() == 0 or !strcmp() for equality).

Note also the functions strncmp() and memcmp() , which are similar to strcmp() but are safer.

You should be using strcmp() :

if (strcmp(players[i].sname, "Lee") == 0) { ...

Also note that strings in C are surrounded by double quotes: "" . Single characters are surrounded by single quotes: '' . I'm not sure exactly what your compiler might be doing with 'Lee' , but it's almost certainly not what you want.

You'd be looking for strcmp() from the header <string.h> .

Note that you need a string — 'Lee' is not a string but a multi-character constant, which is allowed but seldom useful, not least because the representation is defined by the compiler, not the C standard.

If you are looking to compare two strings — call the pointers to them first and second , then you write:

if (strcmp(first, second) == 0)    // first equal to second
if (strcmp(first, second) <= 0)    // first less than or equal to second
if (strcmp(first, second) <  0)    // first less than second
if (strcmp(first, second) >= 0)    // first greater than or equal to second
if (strcmp(first, second) >  0)    // first greater than second
if (strcmp(first, second) != 0)    // first unequal to second

This, in my view, makes it clear what the comparison is and so the notation should be used. Note that strcmp() may return any negative value to indicate 'less than' or any positive value to indicate 'greater than'.

You will find people who prefer:

if (strcmp(first, second))    // first unequal to second
if (!strcmp(first, second))   // first equal to second

IMO, they have the advantage of brevity but the disadvantage of being less clear than the explicit comparisons with zero. YMMV .

Be cautious about using strncmp() instead of strcmp() , which was suggested in one answer. If you have:

if (strncmp(first, "command", 7) == 0)

then if first contains "commander" , the match will be valid. If that's not what you want but you want to use strncmp() anyway, you would write:

if (strncmp(first, "command", sizeof("command")) == 0)

This will correctly reject "commander" .

strcmp works fine, provided one of the strings is null terminated. If both are max length and identical, the comparison will walk off the end, and most likely give a false negative result for the equality test. If one string is fixed inside of "" marks in the strcmp itself, that's not an issue, because we know it's null terminated.

If we are comparing two string variables, and we don't know if they are max length or not (maximum length ones are not null terminated), then we need to use strncmp, and use sizeof on one of them to get the third parameter. This solves the problem, because the sizeof is aware of the maximum length. Strcmp is 100% safe if one of the strings is a literal in double quotes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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