473,385 Members | 1,912 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Style question - using LPCTSTR as a pointer

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
4 3331
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
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
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
<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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Tom | last post by:
Is there such a type in C#. I have a C++ application that uses LPCTSTR to convert a Long Pointer to a Constant String? Thanks Tom
0
by: Dirk Evertz | last post by:
Hi all, I've experienced a problem when passing a smart-pointer (_com_ptr_t) by value to another function using managed extensions. This only seems to happen if the object is created via new....
8
by: ppcdev | last post by:
Here's what I try : LPCTSTR tst = (LPCTSTR) (LPCWSTR) Marshal::StringToHGlobalUni(str); c:\MyNetPrj\Prj0001\stunt.cpp(244): error C2440: 'type cast' : cannot convert from 'System::IntPtr' to...
10
by: farseer | last post by:
How can i do this? i'd like to call the following code: .... string url = <my urld>; TCHAR* urlParams = GetParams( ); url.append( (char * ) urlParams ); GotoURL( ( LPCTSTR ) url ); ...
0
by: Dean Hallman | last post by:
Hello, I am developing a BHO that should add a custom HTTP header on a specific domain only. Don't want the header globally, otherwise I could just add a registry key. So, on...
10
by: Lucy Ludmiller | last post by:
Anyone knows how to convert a LPCTSTR to an STL striung?. Can't seem to finda nyting (that dosen't blab on for several pages) on the net about how to do this
14
by: =?Utf-8?B?Sm9hY2hpbQ==?= | last post by:
I have seen the following function to convert from a System::String^ to a const wchar_t*. I would like to get a LPCTSTR and AFAIK LPCTSTR is equal to const wchar_t*. Then it should all work right?...
7
by: justin001 | last post by:
In my _UNICODE & _MBCS build project, I have LPCTSTR string (i.e.it will be wchar_t*) .The following function is not giving proper result and instead it is producing junk data. I have confirmed it...
1
by: sambarker123 | last post by:
Hi all I am tring to copy a file.The code is as below int _tmain(int argc, _TCHAR* argv) { string s3,s4; s3="C:\\test\\to"; s4="C:\\test\\from\\sam.txt";
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.