Clarifying char* and char[x]..

Recommended Posts

sipickles    240
Hello! Could someone please clarify for me whether there is a difference between: char *myChar; and char myOtherChar[8]; My compiler certainly thinks so. I am infact, getting errors saying cant convert between 'LPCWSTR [64]' to 'wchar_t *' As far as I gather, LPCWSTR and wchar_t/WCHAR are equivalent? So this equates to the above example. Thanks in advance Simon

Share on other sites
SiCrane    11839
LPCWSTR is a const pointer to WCHAR. You can't convert a const pointer to non-const pointer.

As for the difference between char * and char[8], char[8] has a type of array of char with length of 8, and char * is of type pointer to char. char[8] has an implicit decay to char *, but they are not the same type.

Share on other sites
Enigma    1410
Yes, there is a difference. The latter is an array of eight characters, the former is a pointer to a character, or pointer to the first of an (unknown) number of characters. Note that the standard provides an implicit conversion from the latter to the former. Your problem is slightly different. LPCWSTR is a pointer to a constant wchar_t. And LPCWSTR[64] then is an array of 64 pointers to constant wchar_ts. This can be implicitly converted to a wchar_t const * *, but not to a wchar_t *.

Enigma

Share on other sites
sipickles    240
There must be some way of doing this conversion:

WCHAR* g_IP = (CDXUTEditBox*)pControl )->GetText(); //GetText() outputs a LPCWSTR

I am very puzzled!

Thanks

Simon

Share on other sites
SiCrane    11839
You can use a const_cast, but that's probably a bad idea. Why does g_IP need to be a non-const pointer?

Share on other sites
sipickles    240
Well, it can't be const, since the IP address might change, but it could be a non-pointer.

So how does WCHAR = LPWCSTR? err dereferncing it? ( as you can see, I am right muddled!)

Si

Share on other sites
Enigma    1410
WCHAR is a typedef of wchar_t. LPCWSTR is a typedef of wchar_t const *. I do not know CDXUTEditBox so the following is mostly conjecture based on experience of how other edit boxes work. The value returned by ((CDXUTEditBox*)pControl)->GetText() is probably a pointer to the controls internal buffer. If it allowed you to modify the contents of its internal buffer then it would get out-of-sync between what it displayed to the user and what it contained. For this reason changes to the internal buffer must go through SetText, which allows the control to know that its contents have been updated and redraw itself. This is why you cannot store the value returned from GetText in a non-const variable. So, when you get the value from GetText() you will either need to store it in a wchar_t const *, which means that you cannot change it through your variable, or else make a copy, i.e.:
std::wstring g_IP = ((CDXUTEditBox*)pControl)->GetText();

Enigma

Share on other sites
Zahlman    1682
(1) Why are you using wide characters to store an IP address/name?
(2) Why are you not using std::string to represent text data?
(3) A const pointer can point at non-const data. There is a difference between the pointer being const and the pointee being const. That said, you can set a non-const pointer to equal a const pointer value (which is what the array decays to), but not if the pointed-at data is const and you want a pointer to non-const data.
(4) Be aware that if you *did* change stuff via the g_IP pointer, the changes would be seen within the CDXUTEditBox's text buffer, assuming that you're simply returning a pointer to some array data member. This can be a bad idea. If you need to make a copy, make a copy. Using std::string (std::wstring if you really do need wide characters) makes this easy.

Share on other sites
Guest Anonymous Poster
It's not clear to me where you're confused. Lets start off by clearing up all the LPCWSTR nonsense. LPCWSTR is Microsoft shorthand for "const WCHAR *". What you are trying to do is basically:

WCHAR * p = (const WCHAR *) p2;

C++ does not allow this. You cannot assign a const pointer to a non-const pointer. And just in case, const basically means read-only.

So what to do? There are a couple options, the best is to change it to:

const WCHAR * p = (const WCHAR *) p2;

This will work fine assuming you don't need to write to the data. If you do actually need to write to the data then you can't do this (if you try anyway you may crash, corrupt memory, yadda, yadda, etc). In that case you need to *copy* the data first. Again, there are a couple of options but the easiest and most "C++'y" way would be:

std::wstring s = (const WCHAR *) p2;

Now you can party on s to your heart's delight.

Lastly, sometimes you are working with a library that isn't const-correct but you are trying to be a good C++ citizen and do the right thing. In that case, if you are sure nobody is writing to the data you can tell the compiler to stop whining and just deal with it:

WCHAR * p = const_cast((const WCHAR *) p2);

Be very sure you know what you're doing in this case though.

Share on other sites
Anon Mike    1098
That was me btw.

Share on other sites
JohnBolton    1372
Quote:
 Original post by sipicklesThere must be some way of doing this conversion:WCHAR* g_IP = (CDXUTEditBox*)pControl )->GetText(); //GetText() outputs a LPCWSTR

    LPCWSTR g_IP = ( (CDXUTEditBox*)pControl )->GetText();
but the real question is why do you need to convert and what are you trying to convert to?

Share on other sites
sipickles    240
std:wstring works a treat! Sorry that didnt occur to me!

You guys blow me away! Thanks for your patience in the face of such adversity!!

Cheers

Simon