在 C 中使用相等运算符 == 比较两个字符串是否相等
int main (int argc, **argv)
{
if (argv[1] == "-hello")
printf("True\n");
else
printf("False\n");
}
# ./myProg -hello False
为什么?我意识到 strcmp(argv[1], "-hello") == 0
返回 true...但是为什么我不能使用相等运算符来比较两个 C 字符串?
int main (int argc, **argv)
{
if (argv[1] == "-hello")
printf("True\n");
else
printf("False\n");
}
# ./myProg -hello False
Why? I realize strcmp(argv[1], "-hello") == 0
returns true... but why can't I use the equality operator to compare two C strings?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
因为
argv[1]
(例如)实际上是一个指向字符串的指针。所以你所做的就是比较指针。Because
argv[1]
(for instance) is actually a pointer to the string. So all you're doing is comparing pointers.您不能将 C 中的字符串与 == 进行比较,因为 C 编译器实际上并没有关于字符串文字之外的字符串的线索。
编译器看到与两侧的
char*
进行比较,因此它会进行指针比较(比较存储在指针中的地址)You can't compare strings in C with ==, because the C compiler does not really have a clue about strings beyond a string-literal.
The compiler sees a comparison with a
char*
on either side, so it does a pointer comparison (which compares the addresses stored in the pointers)在
C
中,因为在大多数情况下,数组“衰减为指向其第一个元素的指针”。因此,当您拥有数组
"foobar"
并在大多数情况下使用它时,它会衰减为一个指针:您希望它将数组内容与 < em>某事。您可以手动
或自动执行此操作
In
C
because, in most contexts, an array "decays into a pointer to its first element".So, when you have the array
"foobar"
and use it in most contexts, it decays into a pointer:What you want it to compare the contents of the array with something. You can do that manually
or automatically
因为不存在 C 字符串这样的东西。
在 C 中,字符串通常是 char 数组,或指向 char 的指针(几乎相同)。将指针/数组与 const 数组进行比较不会给出预期的结果。
更新:我所说的“没有 C 字符串”是指 C 中没有字符串。通常所说的“C 字符串”是与语言无关的(如“Pascal 字符串”),它是将字符串表示为 null -终止的线性字符数组。
Because there is no such thing as a C string.
In C, a string is usually an array of char, or a pointer to char (which is nearly the same). Comparing a pointer/array to a const array won't give the expected results.
UPDATE: what I meant by 'no C string' is, there is no string in C. What's usually referred to as a 'C string' is language independent (as 'Pascal string' is), it's the representation of strings as a null-terminated linear array of characters.
在 C 中,字符串值(包括字符串文字)表示为
char
数组,后跟 0 终止符,并且不能使用==
运算符来比较数组内容;该语言根本没有定义操作。除非它是
sizeof
或&
运算符的操作数,或者它是用于初始化声明中另一个数组的字符串文字、具有类型的表达式“T 的 N 元素数组”的类型将隐式转换(衰减)为“指向 T 的指针”类型,并且表达式的值将是数组第一个元素的地址。因此,当您编写时,
编译器会隐式地将表达式
"-hello"
从类型“7-element array of char”转换为“pointer to char”(argv[1]
is已经是指针类型),表达式的值是字符'-'
的地址。因此==
最终比较的是两个指针值,它们(很可能)永远不会相等,因为"-hello"
和argv[1](很可能)占据内存中的不同区域。这就是为什么您必须使用
strcmp()
等库函数来比较字符串值。In C, string values (including string literals) are represented as arrays of
char
followed by a 0 terminator, and you cannot use the==
operator to compare array contents; the language simply doesn't define the operation.Except when it is the operand of either the
sizeof
or&
operators, or when it is a string literal being used to initialize another array in a declaration, an expression with type "N-element array of T" will have its type implicitly converted (decay) to type "pointer to T", and the value of the expression will be the address of the first element of the array.So when you write
the compiler implicitly converts the expression
"-hello"
from type "7-element array of char" to "pointer to char" (argv[1]
is already a pointer type), and the value of the expression is the address of the character'-'
. So what==
winds up comparing are two pointer values, which are (most likely) never going to be equal since"-hello"
andargv[1]
(most likely) occupy different regions in memory.This is why you have to use library functions like
strcmp()
to compare string values.因为 C 字符串并不存在。它们是以
\0
结尾的字符数组。相等运算符
==
将测试指向数组第一个元素的指针是否相同。它不会按字典顺序进行比较。另一方面,
"-hello" == "-hello"
可能返回非零,但这并不意味着==
运算符按字典顺序进行比较。这是由于其他事实造成的。如果你想按字典顺序进行比较,你总是可以这样做:
我看到你标记为 C++。所以你可以这样做:
Because C strings don't exist as such. They are char arrays ending in a
\0
.The equality operator
==
will test that the pointer to the first element of the array are the same. It won't compare lexicographically.On the other hand
"-hello" == "-hello"
may return non zero, but that doesn't mean that the==
operator compares lexicographically. That's due to other facts.If you want to compare lexicographically, you can always do this:
I see that you tagged as C++. So you could do this:
字符串不是 C 中的本机类型。您在该示例中比较的是两个指针。一个是第一个参数,另一个是包含“-hello”内容的静态字符数组。
你真的想使用 strncmp 或类似的东西。
Strings are not native types in C. What you are comparing in that example are two pointers. One to your first argument, and the other is a static character array with the contents of "-hello".
You really want to use strncmp or something similar.
当您使用 == 时,您正在比较指针。也就是说,如果两个操作数引用内存中的同一字符串,它将返回 true。因此,它不适合用于按字典顺序比较字符串。
When you're using ==, you're comparing pointers. That is, it will return true if the two operands refer to the same string in memory. Therefore, it's unsuitable for use in comparing strings lexicographically.
因为C字符串是字符数组。数组只是指向数组中第一个元素的指针,当您使用 == 比较两个指针时,它会比较它们指向的内存地址,而不是它们指向的值。
Because C strings are array of characters. Arrays are simply pointers to the first element in the array, and when you compare two pointers using == it compares the memory address they point to, not the values that they point to.