简体   繁体   English

C字符串与等号的比较

[英]C Strings Comparison with Equal Sign

I have this code: 我有这个代码:

char *name = "George"

if(name == "George")
   printf("It's George")

I thought that c strings could not be compared with == sign and I have to use strcmp . 我认为c字符串无法与==符号进行比较,我必须使用strcmp For unknown reason when I compile with gcc (version 4.7.3) this code works. 由于未知原因,当我使用gcc(版本4.7.3)编译时,此代码有效。 I though that this was wrong because it is like comparing pointers so I searched in google and many people say that it's wrong and comparing with == can't be done. 我认为这是错误的,因为它就像比较指针,所以我在谷歌搜索,很多人说这是错误的,与==比较无法完成。 So why this comparing method works ? 那么为什么这种比较方法有效?

I thought that c strings could not be compared with == sign and I have to use strcmp 我认为c字符串无法与==符号进行比较,我必须使用strcmp

Right. 对。

I though that this was wrong because it is like comparing pointers so I searched in google and many people say that it's wrong and comparing with == can't be done 我虽然这是错误的,因为它就像比较指针,所以我在谷歌搜索,很多人说这是错误的,与==比较无法完成

That's right too. 那也是对的。

So why this comparing method works ? 那么为什么这种比较方法有效?

It doesn't "work". 它不“有效”。 It only appears to be working. 似乎只是起作用。

The reason why this happens is probably a compiler optimization: the two string literals are identical, so the compiler really generates only one instance of them, and uses that very same pointer/array whenever the string literal is referenced. 发生这种情况的原因可能是编译器优化:两个字符串文字是相同的,因此编译器实际上只生成它们的一个实例,并且每当引用字符串文字时使用相同的指针/数组。

Just to provide a reference to @H2CO3's answer: 只是提供@ H2CO3答案的参考:

C11 6.4.5 String literals C11 6.4.5 字符串文字

It is unspecified whether these arrays are distinct provided their elements have the appropriate values. 如果这些数组的元素具有适当的值,则这些数组是否不同是未指定的。 If the program attempts to modify such an array, the behavior is undefined. 如果程序试图修改此类数组,则行为未定义。

This means that in your example, name (a string literal "George") and "George" may and may not share the same location, it's up to the implementation. 这意味着在您的示例中, name (字符串文字“George”)和"George"可能也可能不共享相同的位置,这取决于实现。 So don't count on this, it may results differently in other machines. 所以不要指望这一点,它可能会在其他机器上产生不同的结果。

The comparison you have done compares the location of the two strings, rather than their content. 您所做的比较会比较两个字符串的位置 ,而不是它们的内容。 It just so happens that your compiler decided to only create one string literal containing the characters "George" . 只是碰巧你的编译器决定只创建一个包含字符"George"字符串文字。 This means that the location of the string stored in name and the location of the second "George" are the same, so the comparison returns non-zero. 这意味着存储在name中的字符串的位置和第二个"George"位置是相同的,因此比较返回非零。

The compiler is not required to do this, however - it could just as easily create two different string literals, with different locations but the same content, and the comparison would then return zero. 然而,编译器不需要执行此操作 - 它可以轻松地创建两个不同的字符串文字,具有不同的位置但内容相同,然后比较将返回零。

This will fail, since you are comparing two different pointers of two separate strings. 这将失败,因为您正在比较两个不同字符串的两个不同指针。 If this code still works, then this is a result of a heavy optimization of GCC, that keeps only one copy for size optimization. 如果此代码仍然有效,那么这是GCC大量优化的结果,它只保留一个副本以进行大小优化。

Use strcmp() . 使用strcmp() Link . 链接

If you compare two stings that you are comparing base addresses of those strings not actual characters in those strings. 如果比较两个比较,那就是比较这些字符串的基本地址而不是这些字符串中的实际字符。 for comparing strings use strcmp() and strcasecmp() library functions or write program like this. 比较字符串使用strcmp()strcasecmp()库函数或写这样的程序。 below is not a full code just logic required for string comparison. 下面不是完整的代码,只是字符串比较所需的逻辑。

void mystrcmp(const char *source,char *dest)
{
    for(i=0;source[i] != '\0';i++)
        dest[i] = source[i];
   dest[i] = 0;

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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