The problem is that TSTR can be either char or wchar_t, so there's not really much choice beyond allocating an array of TCHAR and copying one into the other with a function that recognizes TCHAR and handles it accordingly:
TCHAR *ttemp = new TCHAR[strlen(temp) + 1]
_tcscpy(ttemp, temp);
deceptikon
Challenge Accepted
3,435 posts since Jan 2012
Reputation Points: 822
Solved Threads: 473
Skill Endorsements: 56
I assume you're working with the Windows API which include types like LPSTR or UINT or LPTSTR.
These are just normal types you're familiar with but given a different name to be used specifically on a windows machine.
ie.
UINT = unsigned int
LPSTR = char*
LP stands for "Long Pointer" (just ignore the Long part, so it's just a pointer)
LPCSTR = const char* (a capital C denotes const keyword)
When you see a capital T (as in LPTSTR) it means the code works with Unicode. You can search up Unicode to find out more but in simple terms it allows for many languages and symbols to be supported.
You can do what deceptikon suggested or just declared the string as
TCHAR temp[10] = "abcd";
and depending on the character set you're working with, TCHAR evaluates to char or wchar_t
dx9_programmer
Junior Poster in Training
61 posts since Mar 2011
Reputation Points: 22
Solved Threads: 12
Skill Endorsements: 0
You can do what deceptikon suggested or just declared the string as
Almost right, you need to surround string literals with _TEXT() macro or _T() macro, they are both the same.
TCHAR temp[10] = _TEXT("abcd");
Ancient Dragon
Achieved Level 70
32,120 posts since Aug 2005
Reputation Points: 5,836
Solved Threads: 2,575
Skill Endorsements: 69
When you see a capital T (as in LPTSTR) it means the code works with Unicode.
Rather, it means that the code accepts either narrow or wide characters. The Win32 API treats wide characters as UTF-16 encoded Unicode, so while it's technically correct to say that it means you're working with Unicode, I still think it's important to make the distinction between wide characters and Unicode characters as well as the variant nature of TCHAR which depends on a compilation setting. ;)
You can do what deceptikon suggested or just declared the string as
Oh, if only it were always that simple. Yes, if you take the question at face value then always using TCHAR is certainly the better option. But I treated it like an example of the issue rather than the actual code and included the possibility that the OP doesn't have any control over the type of temp, which isn't an unreasonable assumption in my view.
deceptikon
Challenge Accepted
3,435 posts since Jan 2012
Reputation Points: 822
Solved Threads: 473
Skill Endorsements: 56