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);
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);
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 stringthe
T
is for a wide character or char (TCHAR) depending on compile options.8-bit AnsiStrings
char
: 8-bit character - underlying C/C++ data typeCHAR
: alias ofchar
- Windows data typeLPSTR
: null-terminated string ofCHAR
(Long Pointer)LPCSTR
: constant null-terminated string ofCHAR
(Long Pointer)16-bit UnicodeStrings
wchar_t
: 16-bit character - underlying C/C++ data typeWCHAR
: alias ofwchar_t
- Windows data typeLPWSTR
: null-terminated string ofWCHAR
(Long Pointer)LPCWSTR
: constant null-terminated string ofWCHAR
(Long Pointer)depending on
UNICODE
defineTCHAR
: alias ofWCHAR
if UNICODE is defined; otherwiseCHAR
LPTSTR
: null-terminated string ofTCHAR
(Long Pointer)LPCTSTR
: constant null-terminated string ofTCHAR
(Long Pointer)So
Bonus Reading
TCHAR
→ Text CharTo answer the second part of your question, you need to do things like
because MS's
LVITEM
struct has anLPTSTR
, i.e. a mutable T-string pointer, not anLPCTSTR
. What you are doing is1) convert
string
(aCString
at a guess) into anLPCTSTR
(which in practise means getting the address of its character buffer as a read-only pointer)2) convert that read-only pointer into a writeable pointer by casting away its
const
-ness.It depends what
dispinfo
is used for whether or not there is a chance that yourListView
call will end up trying to write through thatpszText
. If it does, this is a potentially very bad thing: after all you were given a read-only pointer and then decided to treat it as writeable: maybe there is a reason it was read-only!If it is a
CString
you are working with you have the option to usestring.GetBuffer()
-- that deliberately gives you a writeableLPTSTR
. You then have to remember to callReleaseBuffer()
if the string does get changed. Or you can allocate a local temporary buffer and copy the string into there.99% of the time this will be unnecessary and treating the
LPCTSTR
as anLPTSTR
will work... but one day, when you least expect it...To answer the first part of your question:
LPCSTR
is a const stringLPCTSTR
is aconst TCHAR
string, (TCHAR
being either a wide char or char depending on whether UNICODE is defined in your project)LPTSTR
is a (non-const)TCHAR
stringThis is a great codeproject article describing C++ strings (see 2/3 the way down for a chart comparing the different types)
Adding to John and Tim's answer.
Unless you are coding for 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 compilations. Those are not as relevant today as they used to be.