== is not how we compare two arrays, since that would only compare the addresses:
#include <iostream>
int main()
{
char a[] = "aaa";
char b[] = "aaa";
if (a == b)
std::cout << "Yes" << std::endl;
else
std::cout << "No" << std::endl;
return 0;
}
This code even gives me a warning:
Array comparison always evaluates to false
But when I tried this:
if ("aaa" == "aaa")
It seemed to be working fine. Still gives me a warning, but the warning is:
Condition is always true
At first, I thought it was some kind of caching thing, so I tried a rather unusual string literal:
if ("whuiwhqohqweihqweohi" == "whuiwhqohqweihqweohi")
Still works fine both on MSVC and g++. Is that an implementation dependent behavior? I understand that comparing variables which are known at compile-time is not very useful, but my question is just "how come this is the case?".
Additionally, using auto seems to be also working:
#include <iostream>
int main()
{
auto a = "whuiwhqohqweihqweohi";
auto b = "whuiwhqohqweihqweohi";
if (a == b) {
std::cout << "Yes" << std::endl;
}
else {
std::cout << "No" << std::endl;
}
return 0;
}
This code produces the correct output. What is the type of a and b here?
Please do not answer "Use std::string". It's irrelevant to my question