C String Comparison - equates to true

蹲街弑〆低调 提交于 2019-12-25 00:15:21

问题


Perhaps an odd question..

I'm currently struggling to understand why the following equates to true i.e. "Hello World" is printed to the console? I've always thought that string comparison in C had to be done using strcmp or similar.

char *a = "Hello";
char *b = "Hello";

if(a == b)
{
    printf("Hello World\n");
}

I also thought that this would only equate to true if the addresses were equivalent? Is it the fact that they're literals?

PS. Yes, this is scarcely related to an assignment, but I've just come up with the above off the top of my head. - this doesn't answer the assignment in any way.


回答1:


Identical literals that you put directly into the code will actually be pointed at the same memory location for optimization purposes.

The compiler in this case puts down "Hello" once in fixed memory, then points a and b at that memory.

For a more detailed understanding of what's going on, I suggest you compile this program (add a bunch of string literals first), then use gdb or valgrind or any other memory inspector and manually take a look at the memory pointing to string literals -- you'll find in standard cases gcc puts all the string literals together in memory and re-uses identical string literals.




回答2:


The language makes no requirements where and how string literals are stored. All you know is that they have static storage duration and that you mustn't attempt to change the data. Nothing in the standard requires that two different string literals have different addresses, and it's entirely plausible that an implementation would deduplicate string literal data.

On the other hand, nothing requires that two identical string literals be stored at the same address, so there's little point in comparing addresses. Use strcmp for comparing the content of strings, always.




回答3:


a and b are pointers to characters.

A pointer basically stores a memory address. your a and b variables don't save the word "hello", but they save the memory address at which the word "hello" is saved.

print a and b in your program to see their value. it will look like: 632176 or something like that, and they will be equal.

so the code a == b basically says: "Do a and b point to the same memory address?". And they do, because "hello" is a constant string and it's only going to be written to memory once.




回答4:


I believe if you have two pointers who point to a value that is the same the compiler will just point both pointers at the same piece of memory. Its more efficient. But in C always use strcmp even in these kind of instances.




回答5:


Whats happening here is that you have two pointers, called *a and *b. Next, as you know a pointer points a specific memory location. Now, when both *a and *b are set equal to the exact same sentence, it follows that a and b are pointing to a memory address. However, the compiler notices the pointers are the same sentence, so it sets them pointing to the exact same memory address for optimization purposes.

Because of this, what happens is something like this:

a = 0xFFFFFFFF;
b = 0xFFFFFFFF;
if(0xFFFFFFFF == 0xFFFFFFFF){
    // Code
}

Now when you compare them, the compiler sees it as (0xFFFFFFFF == 0xFFFFFFFF) The compiler sees that they are equal, resulting in the if statement becoming true and showing Hello, World

However, this may not happen with different compilers, so you may get different results across compilers. This behavior does work with gcc though. So in that case, you should always use strcmp for comparisons to avoid random behavior like this.



来源:https://stackoverflow.com/questions/12593115/c-string-comparison-equates-to-true

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