Major brainfreeze while learning C here.
在这里学习C语言的时候大脑会冻结。
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. It's not working atm.
但我运气不太好。是否有一个str函数我应该使用或者是否有它来修正if语句。这不是atm工作。
3 个解决方案
#1
8
The short answer: strcmp()
.
简短的回答: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.
其次,更重要的是,“string1”=“string2”并不比较字符串,而是比较char *s,或指向字符的指针。它将告诉您两个字符串是否存储在相同的内存位置。这意味着它们是相等的,但一个错误的结果并不意味着它们是不相等的。
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).
strcmp()将基本遍历并比较字符串中的每个字符,在第一个不相等的字符处停止,并返回两个字符之间的差异(这就是为什么要为相等而写入strcmp() = 0或!strcmp())。
Note also the functions strncmp()
and memcmp()
, which are similar to strcmp()
but are safer.
还要注意strncmp()和memcmp()函数,它们与strcmp()类似,但更安全。
#2
8
You should be using strcmp()
:
您应该使用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.
还要注意,C中的字符串被双引号包围:“”。单字符被单引号包围:"。我不确定你的编译器可能在用“Lee”做什么,但它肯定不是你想要的。
#3
3
You'd be looking for strcmp()
from the header <string.h>
.
您将从header
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.
注意,您需要一个字符串——'Lee'不是一个字符串,而是一个多字符常量,这是允许的,但很少有用,尤其是因为表示是由编译器定义的,而不是C标准。
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'.
在我看来,这清楚地说明了什么是比较,所以应该使用符号。注意,strcmp()可以返回任何负值来表示“小于”,也可以返回任何正值来表示“大于”。
Be cautious about using strncmp()
instead of strcmp()
, which was suggested in one answer. If you have:
要小心使用strncmp()而不是strcmp(),这是在一个答案中建议的。如果你有:
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:
如果第一个包含“指挥官”,则该匹配将有效。如果这不是您想要的,但是您想使用strncmp(),您可以这样写:
if (strncmp(first, "command", sizeof("command")) == 0)
This will correctly reject "commander"
.
这将正确地拒绝“指挥官”。
#1
8
The short answer: strcmp()
.
简短的回答: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.
其次,更重要的是,“string1”=“string2”并不比较字符串,而是比较char *s,或指向字符的指针。它将告诉您两个字符串是否存储在相同的内存位置。这意味着它们是相等的,但一个错误的结果并不意味着它们是不相等的。
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).
strcmp()将基本遍历并比较字符串中的每个字符,在第一个不相等的字符处停止,并返回两个字符之间的差异(这就是为什么要为相等而写入strcmp() = 0或!strcmp())。
Note also the functions strncmp()
and memcmp()
, which are similar to strcmp()
but are safer.
还要注意strncmp()和memcmp()函数,它们与strcmp()类似,但更安全。
#2
8
You should be using strcmp()
:
您应该使用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.
还要注意,C中的字符串被双引号包围:“”。单字符被单引号包围:"。我不确定你的编译器可能在用“Lee”做什么,但它肯定不是你想要的。
#3
3
You'd be looking for strcmp()
from the header <string.h>
.
您将从header
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.
注意,您需要一个字符串——'Lee'不是一个字符串,而是一个多字符常量,这是允许的,但很少有用,尤其是因为表示是由编译器定义的,而不是C标准。
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'.
在我看来,这清楚地说明了什么是比较,所以应该使用符号。注意,strcmp()可以返回任何负值来表示“小于”,也可以返回任何正值来表示“大于”。
Be cautious about using strncmp()
instead of strcmp()
, which was suggested in one answer. If you have:
要小心使用strncmp()而不是strcmp(),这是在一个答案中建议的。如果你有:
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:
如果第一个包含“指挥官”,则该匹配将有效。如果这不是您想要的,但是您想使用strncmp(),您可以这样写:
if (strncmp(first, "command", sizeof("command")) == 0)
This will correctly reject "commander"
.
这将正确地拒绝“指挥官”。