1

In the lecture of week 4 in CS50x, David illustrates how two strings can't be compared with two equal signs (==), and instead, strcmp() has to be used.

I can't reproduce this locally or on CS50's IDE online. I can compare two strings with ==, as long as I don't create these two strings in runtime using get_string().

The code I'm running:

int main(void)
{
    char *str_1 = "This is a string!";
    char *str_2 = "This is a string!";
    printf("str_1: %s, str_2: %s\n", str_1, str_2);
    printf("str_1 address: %p, str_2 address: %p\n", &str_1, &str_2);
    printf("str_1 == str_2: %d\n", str_1 == str_2);
}

The output:

str_1: This is a string!, str_2: This is a string!

str_1 address: 0x7ffebe62b958, str_2 address: 0x7ffebe62b950

str_1 == str_2: 1

Somehow, I am able to compare strings without strcmp(), and I have no idea why. It's also clear that the two strings are stored in different memory locations, so the pointers are not the same.

Can someone explain this behavior?

(I researched on stackoverflow, and I didn't find anyone asking the same question. In fact I found people asking the opposite: how to compare strings because using == doesn't work for them)

2 Answers 2

1

You can compare the memory addresses of the two strings with == just fine. The point is, this tells you nothing about whether the string contents are equal or not.

10
  • If I understand you well, then you're saying that I am comparing memory addresses on the line printf("str_1 == str_2: %d\n", str_1 == str_2);? If so, why does that result in 1 (true) when the addresses are not the same (str_1 address: 0x7ffebe62b958, str_2 address: 0x7ffebe62b950)? Commented Jan 19, 2022 at 8:38
  • @therubberduck The line from your comment is slightly different from the one in your original example. In the original you take the address of each pointer &str_1, &str_2, and the pointers themselves are stored separately. Without the & you'll see that the address of each string is in fact the same. This is because of a compiler optimization. Both strings in your example are identical, so the compiler only generates one copy and both pointers point to it. Commented Jan 19, 2022 at 9:11
  • Hey @Fuelled_By_Coffee thanks for your response! The lines in my comment and my post are the same, I copy pasted it. I don't understand your comment, specifically: "Without the & you'll see that the address of each string is in fact the same". How can I see the address without the "&"? I understand you're saying the address of the two pointers are the same, but when I print them, they are not (that's why I print them, because I thought that could be the case). What am I missing? Commented Jan 19, 2022 at 10:27
  • Sorry about the confusion. I mean this part this str_1 == str_2 and this &str_1, &str_2 refer to different things. Note the & before your variables in the second version. When you compare them, you compare the values that str_1 and str_2 hold. When you try to print these addresses, you instead print the address of each pointer, not the pointer itself. Commented Jan 19, 2022 at 12:29
  • No worries at all! Ok if I understand your message well, you are saying that printing str_1 prints what is stored at that pointer (and onwards until \0). Printing &str_1 prints the address of the pointer (same address as first char of the string). That all seems clear to me (hope that's correct). I still don't understand though why str_1 == str_2 is true. Commented Jan 20, 2022 at 9:49
0

Running your code on my computer I always get str_1 == str_2: 0. In C++, however, strings can be compared using ==. May be in the library functions you are using the operator == is overloded for char*

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .