Sign in to follow this  
sipickles

Clarifying char* and char[x]..

Recommended Posts

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 this post


Link to post
Share on other sites
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 this post


Link to post
Share on other sites
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 this post


Link to post
Share on other sites
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 this post


Link to post
Share on other sites
(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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by sipickles
There must be some way of doing this conversion:

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

The answer is:

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 this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this