Swapnil Gupta
Swapnil Gupta

Reputation: 8941

Compare TCHAR with String value in VC++?

How to Compare TCHAR with String value in VC++ ? My project is not Unicode. I am doing like this :

TCHAR achValue[16523] = NULL;
if(achValue == _T("NDSPATH"))
            {
                return FALSE;
            }

When achValue = "NDSPATH" then also this condition does not staisfy.

Any help is appreciated.

Upvotes: 4

Views: 14510

Answers (1)

my fat llama
my fat llama

Reputation: 181

A TCHAR or any string array is simply a pointer to the first character. What you're comparing is the value of the pointer, not the string. Also, you're assigning an array to null which is nonsensical.

Use win32 variations of strcmp. If you use the _tcscmp macro, it will use the correct function for multibyte/unicode at compile time.

#define MAX_STRING 16523;

TCHAR achValue[MAX_STRING];
ZeroMemory(achValue, sizeof(TCHAR) * MAX_STRING);

sprintf(achValue, MAX_PATH, _T("NDSPATH"));

if (!_tcscmp(achValue, _T("NDSPATH"))
{
    // strings are equal when result is 0
}

Upvotes: 6

Related Questions