Comparing two LPCSTRs with ==

I found an implementation that compares two LPCSTRs by doing the following:

void check(LPCSTR lpText)
{
    if(lpText == input)
    {
         // do stuff
    }
}

The problem is that it works. I replaced it ...

if(lstrcmpi(lpText, input) == 0)
{
    // do stuff
}

and now I feel safer.

I just wanted to know if the other implementation was just checking addresses or sizes, how does it work?

I checked the memory address of one LPCSTR and 0x0633522c and the other 0x028a91a4.

It shakes my whole foundation.

+4
source share
1 answer

Probably inputin your first example, this is an instance CString, and there is an overload operator==using a C-style string pointer and CString( const CString&), which does the right thing of string comparison.

, cstringt.h ATL :

friend bool operator==(
    _In_z_ PCXSTR psz1,
    _In_ const CStringT& str2) throw()
{
    return( str2.Compare( psz1 ) == 0 );
}
+5

Source: https://habr.com/ru/post/1529453/


All Articles