tags:

views:

2852

answers:

4

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);
+5  A: 

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.

Tim
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.
jalf
that is why I wrote "depending on compile options"
Tim
I really love this type of explaining :) . Thanks so much
nXqd
+7  A: 

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)

John Sibly
I quickly scanned that article - seems great, adding it to my bookmarks and will read it as soon as I have time.
nothingMaster
+1  A: 

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.

JaredPar
What about std::string?
BlueRaja - Danny Pflughoeft
@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.
JaredPar
A: 

Read MSDN where everything is explained , this will avoid noob questions