C Strings Comparison with Equal Sign

匆匆过客 提交于 2019-11-26 09:59:37

问题


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. For unknown reason when I compile with gcc (version 4.7.3) this code works. 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 ?


回答1:


I thought that c strings could not be compared with == sign and I have to use 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.




回答2:


Just to provide a reference to @H2CO3's answer:

C11 6.4.5 String literals

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. So don't count on this, it may results differently in other machines.




回答3:


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". 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.

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.




回答4:


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.

Use strcmp(). Link.




回答5:


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. 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;

}


来源:https://stackoverflow.com/questions/17781855/c-strings-comparison-with-equal-sign

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!