Thursday, March 3, 2011

LPCSTR, LPCTSTR and LPTSTR

What the difference between LPCSTR, LPCTSTR and LPTSTR?
Why do we need to do this to convert a string into a LV_ITEM structure variable pszText:

LV_DISPINFO dispinfo;  
dispinfo.item.pszText = LPTSTR((LPCTSTR)string);
From stackoverflow
  • quick and dirty:

    LP == long pointer. Just think pointer or char*

    C = const in this case i think they mean the character string is a const, not the pointer being const.

    STR is string

    the T is for a wide character or char (TCHAR) depending on compile options.

    jalf : T is not for wide character, it is for varying character type. W is for wide (as in WCHAR). If UNICODE is defined, TCHAR == WCHAR, otherwise TCHAR == CHAR. So if UNICODE is not defined, LPCTSTR == LPCSTR.
    Tim : that is why I wrote "depending on compile options"
    nXqd : I really love this type of explaining :) . Thanks so much
  • To answer the first part of your question:

    LPCSTR is a const string

    LPCTSTR is a const TCHAR string, (TCHAR being either a wide char or char depending on whether UNICODE is defined)

    LPTSTR is a (non-const) TCHAR string

    This is a great codeproject article describing C++ strings (see 2/3 the way down for a chart comparing the different types)

    nothingMaster : I quickly scanned that article - seems great, adding it to my bookmarks and will read it as soon as I have time.
  • Adding to John and Tim's answer.

    Unless you are coding on Win98, there are only two of the 6+ string types you should be using in your application

    • LPWSTR
    • LPCWSTR

    The rest are meant to support ANSI platforms or dual compiles. Those are not as relevant today as they used to be.

    BlueRaja - Danny Pflughoeft : What about std::string?
    JaredPar : @BlueRaja, I was mainly referring to C based strings in my answer. But for C++ I would avoid `std::string` because it is still an ASCII based string and prefer `std::wstring` instead.
  • Read MSDN where everything is explained , this will avoid noob questions

0 comments:

Post a Comment