C++ LPCSTR、LPCTSTR 和 LPTSTR
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/321413/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
LPCSTR, LPCTSTR and LPTSTR
提问by nothingMaster
What the difference between LPCSTR
, LPCTSTR
and LPTSTR
?
什么区别LPCSTR
,LPCTSTR
和LPTSTR
?
Why do we need to do this to convert a string into a LV
/ _ITEM
structure variable pszText
:
为什么我们需要这样做才能将字符串转换为LV
/_ITEM
结构变量pszText
:
LV_DISPINFO dispinfo;
dispinfo.item.pszText = LPTSTR((LPCTSTR)string);
回答by John Sibly
To answer the first part of your question:
要回答问题的第一部分:
LPCSTR
is a pointer to a const string (LP means Long Pointer)
LPCSTR
是一个指向 const 字符串的指针(LP 表示Long Pointer)
LPCTSTR
is a pointer to a const TCHAR
string, (TCHAR
being either a wide char or char depending on whether UNICODE is defined in your project)
LPCTSTR
是指向const TCHAR
字符串的指针,(TCHAR
根据您的项目中是否定义了 UNICODE,可以是宽字符或字符)
LPTSTR
is a pointer to a (non-const) TCHAR
string
LPTSTR
是指向(非常量)TCHAR
字符串的指针
In practice when talking about these in the past, we've left out the "pointer to a" phrase for simplicity, but as mentioned by lightness-races-in-orbit they are all pointers.
在实践中,过去在谈论这些时,为了简单起见,我们省略了“指向 a 的指针”短语,但正如 lightness-races-in-orbit 所提到的,它们都是指针。
This is a great codeproject articledescribing C++ strings (see 2/3 the way down for a chart comparing the different types)
这是一篇很好的代码项目文章,描述了 C++ 字符串(参见 2/3 向下的图表比较不同类型)
回答by Tim
Quick and dirty:
又快又脏:
LP
== Long Pointer. Just think pointer or char*
LP
==大号翁Pointer。想想指针或字符*
C
= Const, in this case, I think they mean the character string is a const, not the pointer being const.
C
= Const,在这种情况下,我认为他们的意思是字符串是一个常量,而不是指针是常量。
STR
is string
STR
是字符串
the T
is for a wide character or char (TCHAR) depending on compile options.
的T
是用于根据编译选项宽字符或字符(TCHAR)。
回答by Ian Boyd
8-bit AnsiStrings
8 位 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)
char
: 8 位字符 - 底层 C/C++ 数据类型CHAR
: 别名char
- Windows 数据类型LPSTR
:的空终止字符串CHAR
(大号翁Pointer)LPCSTR
:恒定空终止字符串CHAR
(大号翁Pointer)
16-bit UnicodeStrings
16 位 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)
wchar_t
: 16 位字符 - 底层 C/C++ 数据类型WCHAR
: 别名wchar_t
- Windows 数据类型LPWSTR
:的空终止字符串WCHAR
(大号翁Pointer)LPCWSTR
:恒定空终止字符串WCHAR
(大号翁Pointer)
depending on UNICODE
define
取决于UNICODE
定义
TCHAR
: alias ofWCHAR
if UNICODE is defined; otherwiseCHAR
LPTSTR
: null-terminated string ofTCHAR
(Long Pointer)LPCTSTR
: constant null-terminated string ofTCHAR
(Long Pointer)
TCHAR
:WCHAR
如果定义了 UNICODE 的别名;除此以外CHAR
LPTSTR
:的空终止字符串TCHAR
(大号翁Pointer)LPCTSTR
:恒定空终止字符串TCHAR
(大号翁Pointer)
So
所以
| Item | 8-bit | 16-bit | Varies |
|-------------------|--------------|-------------|-----------------|
| character | CHAR | WCHAR | TCHAR |
| string | LPSTR | LPWSTR | LPTSTR |
| string (const) | LPCSTR | LPCWSTR | LPCTSTR |
Bonus Reading
奖励阅读
TCHAR
→ Text Char(archive.is)
TCHAR
→文本字符( archive.is)
回答by JaredPar
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
除非您为 Win98 编码,否则您应该在应用程序中使用的 6 种以上字符串类型中只有两种
LPWSTR
LPCWSTR
LPWSTR
LPCWSTR
The rest are meant to support ANSI platforms or dual compilations. Those are not as relevant today as they used to be.
其余的旨在支持 ANSI 平台或双重编译。这些在今天不像过去那样重要。
回答by AAT
To answer the second part of your question, you need to do things like
要回答问题的第二部分,您需要执行以下操作
LV_DISPINFO dispinfo;
dispinfo.item.pszText = LPTSTR((LPCTSTR)string);
because MS's LVITEM
struct has an LPTSTR
, i.e. a mutableT-string pointer, not an LPCTSTR
. What you are doing is
因为 MS 的LVITEM
struct 有一个LPTSTR
,即一个可变的T 字符串指针,而不是一个LPCTSTR
。你正在做的是
1) convert string
(a CString
at a guess) into an LPCTSTR
(which in practise means getting the address of its character buffer as a read-only pointer)
1) 将string
(a CString
at a guess) 转换为 an LPCTSTR
(实际上意味着将其字符缓冲区的地址作为只读指针获取)
2) convert that read-only pointer into a writeable pointer by casting away its const
-ness.
2) 通过丢弃它的const
-ness将该只读指针转换为可写指针。
It depends what dispinfo
is used for whether or not there is a chance that your ListView
call will end up trying to writethrough that pszText
. 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!
这取决于dispinfo
您的ListView
电话是否有可能最终尝试通过该内容写入内容pszText
。如果是这样,这可能是一件非常糟糕的事情:毕竟你得到了一个只读指针,然后决定把它当作可写的:也许它是只读的!
If it is a CString
you are working with you have the option to use string.GetBuffer()
-- that deliberately gives you a writeable LPTSTR
. You then have to remember to call ReleaseBuffer()
if the string does get changed. Or you can allocate a local temporary buffer and copy the string into there.
如果它是CString
你与你的工作需要使用的选项string.GetBuffer()
-故意给你写LPTSTR
。然后,您必须记住ReleaseBuffer()
在字符串确实发生更改时调用。或者您可以分配一个本地临时缓冲区并将字符串复制到那里。
99% of the time this will be unnecessary and treating the LPCTSTR
as an LPTSTR
will work... but one day, when you least expect it...
在 99% 的情况下,这将是不必要的,并且将其LPCTSTR
视为LPTSTR
可行的……但是有一天,当您最不期望它时……