By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,309 Members | 2,035 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,309 IT Pros & Developers. It's quick & easy.

Style question - using LPCTSTR as a pointer

P: n/a
I'm using a system in which TCHAR is typedef-ed to represent a
character, in this case a wchar_t to hold Unicode characters, and
LPCTSTR is typedef-ed to be a pointer to constant wchar_t. I presume
it's supposed to be a pointer to constant TCHAR, though they seem to
be defined in parallel rather than one typedef using the other.

I'm perfectly happy using LPCTSTR for a constant string, but for some
reason it seems odd to use LPCTSTR as an actual pointer. For instance,
in the following snippet:

for(ptr = str; *ptr; ptr++)

I am happy for str to be a LPCTSTR, but ptr feels like it should be a
const TCHAR*. Even though they are of course the same type.

Am I just weird?

Paul.
Aug 9 '08 #1
Share this Question
Share on Google+
4 Replies


P: n/a
TCHAR is a wchar_t if UNICODE is defined. Otherwise it is a plain char.

LPCTSTR is Microsoft nonsense stuff. You can use eg const TCHAR* etc.

Of course, using std::string is much safer.
<gw****@aol.comwrote in message
news:94**********************************@s50g2000 hsb.googlegroups.com...
I'm using a system in which TCHAR is typedef-ed to represent a
character, in this case a wchar_t to hold Unicode characters, and
LPCTSTR is typedef-ed to be a pointer to constant wchar_t. I presume
it's supposed to be a pointer to constant TCHAR, though they seem to
be defined in parallel rather than one typedef using the other.

I'm perfectly happy using LPCTSTR for a constant string, but for some
reason it seems odd to use LPCTSTR as an actual pointer. For instance,
in the following snippet:

for(ptr = str; *ptr; ptr++)

I am happy for str to be a LPCTSTR, but ptr feels like it should be a
const TCHAR*. Even though they are of course the same type.

Am I just weird?

Paul.

Aug 9 '08 #2

P: n/a
gw****@aol.com wrote:
I'm using a system in which TCHAR is typedef-ed to represent a
character, in this case a wchar_t to hold Unicode characters, and
LPCTSTR is typedef-ed to be a pointer to constant wchar_t. I presume
it's supposed to be a pointer to constant TCHAR, though they seem to
be defined in parallel rather than one typedef using the other.

I'm perfectly happy using LPCTSTR for a constant string, but for some
reason it seems odd to use LPCTSTR as an actual pointer. For instance,
in the following snippet:

for(ptr = str; *ptr; ptr++)

I am happy for str to be a LPCTSTR, but ptr feels like it should be a
const TCHAR*. Even though they are of course the same type.

Am I just weird?

Paul.
It's a microsoft-specific quite, but the answer could be that TCHAR
under UNICODE is defined to WCHAR, not wchar_t and WCHAR is *not
always* :-) same as wchar_t. See
http://msdn.microsoft.com/en-us/libr...97(VS.85).aspx for the
details.

In the future for the problems like this I advice you to pre-process
your code and look into the output of the pre-processor to get a
definitive answer on what the identifiers you use really mean.

Hope this will help,
-Pavel
Aug 9 '08 #3

P: n/a
gw****@aol.com wrote:
I'm using a system in which TCHAR is typedef-ed to represent a
character, in this case a wchar_t to hold Unicode characters, and
LPCTSTR is typedef-ed to be a pointer to constant wchar_t. I presume
it's supposed to be a pointer to constant TCHAR, though they seem to
be defined in parallel rather than one typedef using the other.

I'm perfectly happy using LPCTSTR for a constant string, but for some
reason it seems odd to use LPCTSTR as an actual pointer. For instance,
in the following snippet:

for(ptr = str; *ptr; ptr++)

I am happy for str to be a LPCTSTR, but ptr feels like it should be a
const TCHAR*. Even though they are of course the same type.

Am I just weird?
I'm weighing in a little late but...

In general if I'm using assignment, I would not change the type unless I
had an overwhelming reason to do so. So for your example, if 'str' is a
LPCTSTR then I would make 'ptr' and LPCTSTR. To do anything else, makes
assumptions about the type that you simply should not make.

Of course, there is the issue that James Kanze raised, maybe 'str'
shouldn't be an LPCTSTR in the first place. I don't do Windows
programming so I can't answer that question.
Aug 10 '08 #4

P: n/a
<gw****@aol.comwrote in message news:94**********************************@s50g2000 hsb.googlegroups.com...
I'm using a system in which TCHAR is typedef-ed to represent a
character, in this case a wchar_t to hold Unicode characters, and
LPCTSTR is typedef-ed to be a pointer to constant wchar_t.
TCHAR is a Microsoft Platform SDK (the SDK you use to write Win32 / Win64 Apps) defined type that, depending on the definition of "UNICODE" holds either an ansi (CHAR), or unicode (WCHAR) character.

LPCTSTR is a long pointer (LP) to a const (C) ansi or unicode text (T) zero terminated string (STR).

Also, even when TCHAR or TSTR is a WCHAR or WSTR, they still don't really point to wchar_t - the size of wchar_t is left up to the compiler to define - GCC for example defines wchar_t to be 32 bits. There are gcc environments that target windows so thsi problem while largely theroetical, cannot be ignored if you need to write cross platform code that interoperates with windows types or APIs.
I presume it's supposed to be a pointer to constant TCHAR, though
they seem to be defined in parallel rather than one typedef using
the other.
They are defined in parallel because the *STR pointers point to strings - arrays of characters that are zero termianted, while *CHAR doesnt imply more than 1 character.
I'm perfectly happy using LPCTSTR for a constant string, but for some
reason it seems odd to use LPCTSTR as an actual pointer. For instance,
in the following snippet:

for(ptr = str; *ptr; ptr++)

I am happy for str to be a LPCTSTR, but ptr feels like it should be a
const TCHAR*. Even though they are of course the same type.
LPCTSTR is logically more correct because you are enumerating characters in a string.


Aug 12 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.