How does the comparison operator work when doing a comparison between two character arrays?
Consider the following program,
#include <iostream>
using namespace std;
int main()
{
char arr1[5] = { 'T','e','s','t' };
char arr2[10] = { 't','e' };
if (arr1 < arr2)
cout << "Yes";
else if (arr1 > arr2)
cout << "No";
else
cout << "0";
}
The way I know is it should print Yes
because the first character of arr1
has an ASCII value of 84
while the ASCII value of first character of arr2
is 116
so technically it should print Yes.
However, this program gives the output of No
when run on Visual Studio.
I thought that it might be comparing the addresses of character arrays. To test this, I swapped the variable names and ran this program,
#include <iostream>
using namespace std;
int main()
{
char arr2[5] = { 'T','e','s','t' };
char arr1[10] = { 't','e' };
if (arr1 < arr2)
cout << "Yes";
else if (arr1 > arr2)
cout << "No";
else
cout << "0";
}
But this again gave Yes
as output, meaning the addresses of character arrays did not matter in the comparison.
Can anyone tell how is this comparison being done?
CodePudding user response:
In the condition of the if statement
if (arr1 < arr2)
the both arrays are implicitly converted to pointers to their first elements and these addresses are compared that results in undefined behavior for the operator <
.
Pay attention to if you will compare string literals like
if ( "Hello" == "Hello" )
//...
then the value of the expression can be either true or false dependent on compiler options: whether identical string literals are stored as one string literal or as separate string literals.
As for the comparison of character arrays that do not contain strings then you can use the standard algorithm std::lexicographical_compare
.
Otherwise if character arrays contain strings as in your example then you can use the standard C string function strcmp
.
Here is a demonstration program
#include <iostream>
#include <algorithm>
#include <cstring>
int main()
{
char arr1[5] = { 'T','e','s','t' };
char arr2[10] = { 't','e' };
if (std::lexicographical_compare( arr1, arr1 4, arr2, arr2 2 ))
{
std::cout << "The array arr1 is less than the array arr2\n";
}
if ( std::strcmp( arr1, arr2 ) < 0 )
{
std::cout << "The array arr1 is less than the array arr2\n";
}
}
The program output is
The array arr1 is less than the array arr2
The array arr1 is less than the array arr2
CodePudding user response:
It definitely compares addresses and not values of array items. You should not rely on address allocations in this manner.
Anyways, you can check:
- string.h for strcmp() // need to terminate your char arrays with '\0'
- string::compare()
- std::lexicographical_compare()
CodePudding user response:
Consider using string_view, it has hardly any overhead. https://en.cppreference.com/w/cpp/string/basic_string_view
note : this example compares the strings at compile time for easy testing, but it works at runtime too (without the constexpr)
#include <cassert>
#include <string_view>
int main()
{
constexpr std::string_view cvw1{ "test" };
constexpr std::string_view cvw2{ "te" };
static_assert(cvw1 > cvw2);
return 0;
}