Firstly, consider this example:
#include <iostream>
using namespace std;
int main()
{
cout << ("123" == "123");
}
What do I expect: since "123" is a const char*
, I expect ADDRESSES (like one of these answers said) of these strings to be compared.
... because
!=
and==
will only compare the base addresses of those strings. Not the contents of the strings themselves.
But still the output is 1
. Okay, we actually don't know how to compare addresses of two prvalue objects (or at least I don't understand how it would be done). So let's declare these strings as variables and see what will happen:
#include <iostream>
using namespace std;
int main()
{
const char* a = "1230";
const char* b = "1230";
cout << (a == b);
}
Still the output is 1
. So const char*
strings does not decay? Or compiler managed to do some optimizations and allocate memory only for one string? Ok, let's try to avoid them:
#include <iostream>
using namespace std;
int main()
{
const char* a = "1230";
const char* b = "1231";
b = "1230";
cout << (a == b);
}
Still the result is the same. Which made me think that const char*
really does not decays. But that didn't made my life simpler. How then const char*
s are compared?
Why here the output is 1
:
#include <iostream>
using namespace std;
int main()
{
const char* a = "1230";
const char* b = "1231";
cout << (a > b);
}
a
is less than b
, in terms of lexographical comparison, but here a
is bigger. How then comparison of const char*
s is implemented?