Style question - using LPCTSTR as a pointer

G

gw7rib

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.
 
A

Angus

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.
 
P

Pavel

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/library/ms684497(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
 
J

James Kanze

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.

Or in his case, std::wstring.

FWIW: the whole idea of typedef'ing the character type seems a
bit wrong, somehow. Unless it is something along the lines:
typedef int32_t UTF32Char ;
typedef uint8_t UTF8Byte ;
etc. The idea that you can switch between wide characters and
narrow just by changing a typedef is simply wrong; in practice,
your code must know the actual type, and even the actual
encoding, in order to work.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top