Windows C++: LPCTSTR vs const TCHAR

user353297 picture user353297 · Jun 7, 2010 · Viewed 9.4k times · Source

In my application i'm declaring a string variable near the top of my code to define the name of my window class which I use in my calls to RegisterClassEx, CreateWindowEx etc.. Now, I know that an LPCTSTR is a typedef and will eventually follow down to a TCHAR (well a CHAR or WCHAR depending on whether UNICODE is defined), but I was wondering whether it would be better to use this:

static LPCTSTR szWindowClass = TEXT("MyApp");

Or this:

static const TCHAR szWindowClass[] = TEXT("MyApp");

I personally prefer the use of the LPCTSTR as coming from a JavaScript, PHP, C# background I never really considered declaring a string as an array of chars.

But are there actually any advantages of using one over the other, or does it in fact not even make a difference as to which one I choose?

Thank you, in advanced, for your answers.

Answer

Mark Ransom picture Mark Ransom · Jun 7, 2010

The two declarations are not identical. The first creates a pointer, the second an array of TCHAR. The difference might not be apparent, because an array will decompose into a pointer if you try to use it, but you'll notice it instantly if you try to put them into a structure for example.

The equivalent declaration to LPCTSTR is:

static const TCHAR * szWindowClass = TEXT("MyApp");

The "L" in LPCTSTR stands for "Long", which hasn't been relevant since 16-bit Windows programming and can be ignored.