I tried to compare unsigned char array with const char*
and it doesn't work, how to solve this?Code:if(str == ">>") { }
I tried to compare unsigned char array with const char*
and it doesn't work, how to solve this?Code:if(str == ">>") { }
The problem is that an in most contexts, array is converted to a pointer to its first element, so you end up comparing pointers rather than strings.
Why do you have an array of unsigned char in particular? One of the possible solutions to the problem is to change str to be a std::string, upon which the str == ">>" comparison will just work.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
C++ string literals are of type pointer to const char (const char*). When comparing two pointers, it is their addresses that get compared, not the contents pointed to. Therefore, you cannot use operator == to compare C-style strings.
Possible solutions:
a) When programming in C, you should use the strcmp function
b) Use std::string from C++ (convert some C-style strings to std::string)
Here is an example of a quick fix:
or:Code:#include <string> using std::string; ... if (str==string("<<") )
Code:#include <string> using std::string; ... string str = "some string"; if (str=="<<")
Last edited by Kevin C; 09-10-2015 at 07:27 AM.
That is not accurate:Originally Posted by Kevin C
Originally Posted by C++11 Clause 2.14.5 Paragraph 8
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)