4

I have the following code:

LPCTSTR strPermission = Method();

if (strPermission == L"0")
{
    return true;
}
else
{
    return false;
}

While debugging I can see that strPermission does equal "0", yet when I compare it like in the if statement it always returns false.

The only thing I can think of is that it is comparing the memory address of the variable rather than the variable value.

How do I compare strPermission to L"0" so that it would return true if strPermission equals "0".

Thank you!

Landin Martens
  • 3,283
  • 12
  • 43
  • 61
  • 1
    LPCTSTR is either `const char*` or `const wchar_t*`, so think about it. Also, shouldn't you be checking for NULL or the null terminator and not the string literal "0"? – Jesse Good Mar 20 '12 at 23:25
  • You can use C-style string-comparison functions, but use the version for TCHAR, which is [tcscmp](http://msdn.microsoft.com/en-us/library/e0z9k731(v=vs.80).aspx). – chris Mar 20 '12 at 23:26
  • 1
    Oops, I meant `_tcscmp`, if you tried the first one to some disappointment. – chris Mar 20 '12 at 23:37
  • possible duplicate of [How to compare pointer to strings in C](http://stackoverflow.com/questions/3663668/how-to-compare-pointer-to-strings-in-c) – Mahmoud Al-Qudsi Mar 20 '12 at 23:44
  • Close, but these data types are a bit more specific. That is the main issue here though. – chris Mar 20 '12 at 23:46
  • May one ask what the circumstances are that force you to support Windows 9x (by using the T stuff)? – Cheers and hth. - Alf Mar 20 '12 at 23:53
  • For me I create a CString variable and use `.Compare()` to compare `LPCTSTR` – Deqing Oct 11 '16 at 08:40

3 Answers3

4

You'll need to use a C runtime library function. strcmp compares ANSI strings, wcscmp compares UNICODE strings.

You use it like this:

bool match = wcscmp(strPermission, L"0") == 0;
AVIDeveloper
  • 2,954
  • 1
  • 25
  • 32
2

You can't compare C-style strings like that in C or C++. Check out this C FAQ question & answer.

The function you're looking for is called lstrcmp.

Carl Norum
  • 219,201
  • 40
  • 422
  • 469
1

LPCTSTR is a pointer to an array of const wchar_t. strPermission points to the first character of the array. L"0" is a string literal, which is an array of const wchar_t, which decays to a pointer of const wchar_t. But the pointers are not equal, they point to different arrays. This is why we invented C++. Please use it.

std::wstring strPermission = Method();
return (strPermission == L"0"); //works like magic!

or, if Method is returning something you have to retain, at least do this

std::unique_ptr<wchar_t[]> strPermission = Method();
return (std::wcscmp(strPermission.get(), L"0")==0); 
//wcscmp is for comparing strings, but returns 0 if they're equal.

Also, are you sure that strPemission points to an array that contains a zero character followed by a null character? If not, and you're not using wstring, then you also have to check that it points at an array

if (strPermission)
     //do stuff
else
     //its a NULL pointer.

I have been prodded by chris to point out that the type of LPCTSTR actually depends on the compiler options. I can tell by your code that you're coding with _UNICODE set, which makes it a const wchar_t*, but if you want to be able to compile with other options (I can't think of a good reason to do so) you should use _tcscmp to compare, have the literals as _T("0") and they'll be arrays of TCAHR. For the strings, you'll have to add a typedef somewhere:

#ifdef _UNICODE
    typedef std::string std::tstring 
    //you'll probably have to add more t helper functions here
#else
    typedef std::string std::wstring
    //you'll probably have to add more t helper functions here
#endif

If you want to be certain that your code is always _UNICODE (which is what I do), explicitly call MethodW() instead of Method(). (There is also a corresponding MethodA(), but not much reason to call it).

There's also a UNICODE macro, but it should always be the same as the _UNICODE macro. (Never define these yourself, they belong in the project options)

Mooing Duck
  • 64,318
  • 19
  • 100
  • 158
  • The thing is that although you can assume LPCTSTR is a LPCWSTR because of the L"0" in the question, it is technically a `const TCHAR *`. – chris Mar 20 '12 at 23:35
  • @chris: technically it is, but I ignored that. I'll add a comment. – Mooing Duck Mar 20 '12 at 23:36