Click here to Skip to main content
15,884,099 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
How to assign a variable of type wchar_t to a temporary variable ?

In my program values for variables of type wchar_t are obtained dynamically and I want to assign them to a tempory variable of the same type for formatting purpose.
But I get an error, that I cannot convert wchar_t to wchar_t.
For example:
C++
wchar_t    str1[2046];
swscanf_s(buffer, _T("%[^,]"), str1, 2046);

wchar_t strId;
strId = str1; // this is not allowed 



How can I assign them?
Posted

wchar_t is a single Unicode character (16 bits). For instance, your strId.
An array of wchar_t (like your str1) can hold a string.
Consider using std::wstring to hold strings.
Other (worse) options are: using a pointer (wchar_t *pStr = str1) which is never a good idea, and in your particular case is a really bad idea; or, if you're totally and absolutely convinced that the length of your str1 variable is one and only one character, you can do: wchar_t strId = str1[0].

Hope this helps,

Pablo.
 
Share this answer
 
Comments
Sumal.V 13-Jun-12 6:52am    
Cheers, but when I used std::wstring strId;
strId = str1;
The format / value is completely different. The value obtained dynamically is "21:00:55" but the strId value holds "4294967295"
Sumal.V 13-Jun-12 6:56am    
And it is the npos value its taking..
Sumal.V 13-Jun-12 7:27am    
Thank u :)
using a pointer
wchar_t *pStr = str1;

you can try
 
Share this answer
 
Comments
Sumal.V 13-Jun-12 7:28am    
cheers :)that worked
nv3 13-Jun-12 9:18am    
Can't have worked. You just stored a pointer to the original string in str1. If str1 changes, the string you see in pStr changes. Hence you didn't get a temporary copy as you intended. So be careful.
Sumal.V 13-Jun-12 11:38am    
I assigned the dynamic value to a pointer and it works...
wchar_t *pStrId = str1;

//pStrId now takes the correct values.
nv3 13-Jun-12 12:06pm    
Of course pStrId shows the same value as in str1. But is not a copy, but just a pointer to the same buffer. If you don't believe me, make the following test. Modify the first character in str1, e.g. by str1[0] = 'X'; Then look at what pStrId shows you: It will be the modified string, not the original one.

Also, if str1 goes out of scope, pStrId will point to undefined storage.

What do you need the copy for? Perhaps that will give an answer to the best technique to use in that case.
The obvious problem is that str1 is an array of wchat_t and strId is a single wchar_t. So it's impossible to assign str1 to strId and the compiler is complaining for a good reason.

Now, the first step is to make strId and array of the same size as str1, so write:

wchar_t strId[2046];


The compiler will still not copy str1 into strId, because array copy is not a basic language construct. And in addition you don't want to copy all 2046 elements of str1, but only those up to and including the NULL character. But a library function comes to help, which is called _tcscpy_s, which does just that:

_tcscpy_s (strId, 2046, str1);


Keeping track of the length of such arrays, allocating them and copying them is kind of unpleasant task. The STL library type std::wstring takes away all that burden and makes things so much easier. The problems you are still having with wstring result from not enough experience with using this class. I'd recommend you take a look at the documentation and some examples and everything will work out fine. If you still are not in luck, paste the exact code you are using so we can have a look at it.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900