“一些”和“一些”之间的区别是什么。 ==“some\0”和c ++中的strcmp(“some”,“some\0”)? [英] what is the difference between "some" == "some\0" and strcmp("some","some\0") in c++?
问题描述
some==some\0
和 strcmp(some,some\0 c>
为什么 if(some==some\0 code>返回
false
和 if(!strcmp(some,some\0))
返回 true
?
推荐答案
当你执行 if some==some\0)
你正在比较的地址。它被转换为 if(0xdeadbeef == 0x0badcafe)
这显然是false。
When you're doing if("some" == "some\0")
you are comparing the addresses. It is translated into if (0xdeadbeef == 0x0badcafe)
which is obviously false.
$ c> strcmp ,您可以比较每个框的内容,直到到达每个框中的 \0
。
When you use strcmp
, you compare the content of each box until you reach \0
in each of them. That's why the second test returns true.
如果您将第一个测试更改为 if(some==some)
然后编译器可能潜在地
看到它们是相同的字符串,并且将只存储它们一次。这意味着你的测试将转换为 if(0x0badcafe == 0x0badcafe)
这显然是真的。
If you change the first test to if("some" == "some")
then a compiler may potentially
see that they are the same strings and will store them only once. Which means that your test will transform into if (0x0badcafe == 0x0badcafe)
which is obviously true.
这篇关于“一些”和“一些”之间的区别是什么。 ==“some\0”和c ++中的strcmp(“some”,“some\0”)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!