将字符数组与 C 中的 == 运算符进行比较
我知道在 C 中比较“字符串”的正确方法是使用 strcmp
,但现在我尝试将一些字符数组与 ==
运算符进行比较,并得到一些奇怪的结果结果。
看一下下面的代码:
int main()
{
char *s1 = "Andreas";
char *s2 = "Andreas";
char s3[] = "Andreas";
char s4[] = "Andreas";
char *s5 = "Hello";
printf("%d\n", s1 == s2); //1
printf("%d\n", s3 == s4); //0
printf("%d\n", s1 == s5); //0
}
第一个 printf
正确打印 1
,这表明它们不相等。但是有人可以向我解释为什么在比较字符数组时 ==
返回 0
吗?
有人可以向我解释为什么第一个 < code>printf 返回 1
(即它们相等),而字符数组返回 0
?
I know that the correct way to compare "strings" in C is by using strcmp
, but now I tried comparing some character arrays with the ==
operator, and got some strange results.
Take a look at the following code:
int main()
{
char *s1 = "Andreas";
char *s2 = "Andreas";
char s3[] = "Andreas";
char s4[] = "Andreas";
char *s5 = "Hello";
printf("%d\n", s1 == s2); //1
printf("%d\n", s3 == s4); //0
printf("%d\n", s1 == s5); //0
}
The first printf
correctly prints a 1
, which signals that they are not equal. But can someone explain to me why, when comparing the character arrays, the ==
is returning a 0
?
Can someone please explain to me why the first printf
is returning a 1
(ie, they are equal) and the character arrays are returning a 0
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
== 是比较内存地址。
您的编译器可能使 s1 和 s2 指向相同的静态数据以节省空间。
IE。前两行代码中的“Andreas”存储在您的可执行数据中。 C 标准表示这些字符串是常量,因此优化了这两个指针以指向同一存储空间。
char[] 行通过将数据复制到变量中来创建变量,因此在执行期间存储在堆栈上的不同地址。
The == is comparing the memory address.
It's likely that your compiler is making s1 and s2 point to the same static data to save space.
ie. The "Andreas" in the first two lines of code is stored in your executable data. The C standard says these strings are constant and so has optomized the two pointers to point to the same storage.
The char[] lines create a variable by copying the data into the variable and so are stored at different address on the stack during execution.
呃...当
==
打印 1 时,意味着它们相等。它与 strcmp 不同,strcmp 返回字符串的相对顺序。Uh... when
==
prints a 1, it means they are equal. It's different from strcmp, which returns the relative order of the strings.您正在比较地址而不是字符串。前两个是常量,只会创建一次。
在我的计算机上输出,但你明白了:
You are comparing addresses and not the strings. The first two are constant and will only be created once.
Output on my computer, but you get the idea:
等一下...1 表示 true,0 表示 false。所以你的解释部分是倒退的。至于为什么前两个字符串看起来相等:编译器只构建了一次常量字符串 (s1/2)。
Wait a sec... 1 means true, 0 means false. So your explanation is partially backwards. As for why the first two strings seem to be equal: The compiler built that constant string (s1/2) just once.
s1 == s2
表示“(char*) == (char*)
”或者地址相同。对于
s3 == s4
也是如此。这就是“数组衰减为指针”在起作用。而你的比较结果的含义是错误的:
s1 == s2
means "(char*) == (char*)
" or that the addresses are the same.Same thing for
s3 == s4
. That's the "arrays decay into pointers" at work.And you have the meaning of the result of the comparison wrong:
从 s1 到 s5 的所有值本身都不是 char,它们是char 的指针。因此,您要比较的是每个字符串的内存地址,而不是字符串本身。
如果您这样显示地址,您可以看到比较运算符实际上在做什么:
字符串在内存中的确切分配位置是特定于实现的。在这种情况下, s1 和 s2 指向相同的静态内存块,但我不希望这种行为是可移植的。
All the values from s1 through s5 aren't char themselves, they're pointers to char. So what you're comparing is the memory addresses of each string, rather than the strings themselves.
If you display the addresses thus, you can see what the comparison operators are actually working on:
Exactly where the strings are allocated in memory is implementation specific. In this case, the s1 and s2 are pointing to the same static memory block, but I wouldn't expect that behaviour to be portable.
您无法比较字符串,但可以比较指针。
You can't compare strings, but you can compare pointers.