Thread: Compare unsigned char array with const char*

  1. #1
    Registered User
    Join Date
    Jun 2015
    Posts
    14

    Compare unsigned char array with const char*

    I tried to compare unsigned char array with const char*

    Code:
    if(str == ">>")
    {
    
    }
    and it doesn't work, how to solve this?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jul 2015
    Posts
    37
    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:

    Code:
    #include <string>
    using std::string;
    ...
    
        if (str==string("<<") )
    or:

    Code:
    #include <string>
    using std::string;
    ...
        string str = "some string";
        if (str=="<<")
    Last edited by Kevin C; 09-10-2015 at 07:27 AM.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Kevin C
    C++ string literals are of type pointer to const char (const char*).
    That is not accurate:
    Quote Originally Posted by C++11 Clause 2.14.5 Paragraph 8
    Ordinary string literals and UTF-8 string literals are also referred to as narrow string literals. A narrow string literal has type "array of n const char", where n is the size of the string as defined below, and has static storage duration.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 07-24-2012, 10:41 AM
  2. Replies: 2
    Last Post: 10-06-2009, 09:37 AM
  3. Replies: 7
    Last Post: 04-28-2008, 09:20 AM
  4. Convert `const unsigned char []' to `std::string'
    By Russell in forum C++ Programming
    Replies: 15
    Last Post: 07-18-2005, 07:35 PM
  5. casting const unsigned char * to string
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 06-06-2002, 08:51 AM

Tags for this Thread