4

Can someone explain to me why txt3, txt4 and txt4, txt5 are not equal?

Why does (txt3 == txt4) and (txt5 == txt6) give false?

#include <iostream>
#include <string>
using namespace std;

int main()
{
  string txt1("EL020GM");
  string txt2("EL020GM");

  char txt3[] = "EL020GM";
  char txt4[] = "EL020GM";

  char * txt5 = &txt3[0];
  char * txt6 = &txt4[0];

  const char * txt7 = "EL020GM";
  const char * txt8 = "EL020GM";

  if(txt1 == txt2)  { std::cout << "yes\n";  }  else{ std::cout << "no\n"; }
  if(txt3 == txt4)  { std::cout << "yes\n";  }  else{ std::cout << "no\n"; }
  if(txt5 == txt6)  { std::cout << "yes\n";  }  else{ std::cout << "no\n"; }
  if(txt7 == txt8)  { std::cout << "yes\n";  }  else{ std::cout << "no\n"; }

  return 0; 
}
3
  • std::cout << (txt1 == txt2 ? "yes" : "no") << '\n'; But like the answer says, your Boolean expression needs fixing.
    – sweenish
    Mar 7, 2022 at 20:23
  • 1
    C strings are materialized by pointers onto their first character in memory. What makes txt7 and txt8 equals is that they are declared const, hence immutable. If they are assigned the same string, the compiler knows it can safely make them point to the same place, unlike txt3 and txt4 which can change over time, hence needing each their own space in memory.
    – Obsidian
    Mar 7, 2022 at 20:52
  • @Obsidian I think your explanation over-thinks matters. And you make it sound like a virtue out of conscious intent. Whereas I believe, it is a minor design flaw, involving implicit type conversion. bool operator== (const std::string& left, const std::string& right) { ... } likely exists (or something similar), but I doubt, there exists bool operator==(const std::string& left, const char* right) { return false; } or similar or more generic. C++ has its flaws and there is no benefit in giving glorified excuses for such pathological situations.
    – BitTickler
    Mar 7, 2022 at 20:57

2 Answers 2

13

Because you can't use == to compare C-style strings. You have to use strcmp():

if (strcmp(txt1, txt2) == 0) ...

That's how you do a string comparison of C-style strings.

Or better yet, use C++ strings.

What your code is doing is asking if the POINTERS are pointing to the same place. They don't. You are not comparing the data being pointed at.

4

Below are C strings (an array of characters)

char txt3[] = "EL020GM";
char txt4[] = "EL020GM";

means txt3 is a located at some address and is holding the group of characters "EL020GM" and txt4 is located at some other address and is holding the group of characters "EL020GM".

Assume that txt3 is located at address 1000, E is stored in address 1000, L is at 1001 ...

Same way assume txt4 is located at address 2000, E is stored in address 2000, L is at 2001 ...

txt3 == txt4 

This means you are asking if both addresses are same. They are not. So it will return false.

Now,

char * txt5 = &txt3[0];
char * txt6 = &txt4[0];

txt5 contains the address of first character in txt3 which is 1000 (assume txt3 is located at address 1000 and txt4 located at 2000). txt6 contains the address of first character in txt4 which is 2000.

txt5 == txt6

means you are asking whether the addresses of first characters in txt3, txt4 are same. They are not. So it is also returning false.

If you want to compare c strings, you need to use strcmp().

C++ strings are different.

string txt1("EL020GM");
string txt2("EL020GM");

C++ strings are of type 'string' class. This class internally maintains a buffer to hold the string and the class also overloads "==" operator.

When you compare C++ strings, the overloaded "==" operator function will be invoked and the function will compare the characters (one by one) stored in the buffers of the string objects and returns true if all characters are same, else it will return false.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.