Trying to get wchar_t... from a lookup array but type error... pls help!

J

Julius Mong

Hi all, I'm doing this:

// Test char code
wchar_t lookup[] = {0x8364, 0x5543, 0x3432, 0xabcd, 0xef01};
for (int x=0; x<5; x++) {
wchar_t * string = (wchar_t*) malloc(sizeof(wchar_t));
string = (wchar_t*)lookup[1];
string[sizeof(wchar_t)] = '\0';
CComBSTR bstrTest = SysAllocString(string);
}

And the line string=lookup[...] gives me this error:

error C2440: '=' : cannot convert from 'unsigned short' to 'unsigned
short *'

Can someone tell me what I'm missing? I've tried casting it to (wchar_t*)
and it'd crash as expected... I'm stuck...

Thanks, Jules
 
J

John Harrison

Julius Mong said:
Hi all, I'm doing this:

// Test char code
wchar_t lookup[] = {0x8364, 0x5543, 0x3432, 0xabcd, 0xef01};
for (int x=0; x<5; x++) {
wchar_t * string = (wchar_t*) malloc(sizeof(wchar_t));
string = (wchar_t*)lookup[1];
string[sizeof(wchar_t)] = '\0';
CComBSTR bstrTest = SysAllocString(string);
}

And the line string=lookup[...] gives me this error:

error C2440: '=' : cannot convert from 'unsigned short' to 'unsigned
short *'

Can someone tell me what I'm missing?

Well lookup[1] is a wchar_t and string is a wchar_t*, as the error message
says. VC++ incorrectly thinks wchar_t is the same as unsigned short, at
least that's incorrect in C++, I'm not sure about C.

I've tried casting it to (wchar_t*)
and it'd crash as expected... I'm stuck...

The next two lines of you code are incorrect as well, it obvious you don't
understand pointers very well yet. Here's how you can do it.

wchar_t lookup[] = {0x8364, 0x5543, 0x3432, 0xabcd, 0xef01};
for (int x=0; x<5; x++) {
wchar_t string[2];
string[0] = lookup[1];
string[1] = '\0';
CComBSTR bstrTest = SysAllocString(string);
}

Changes from your code

1) I use an array of TWO characters (one for the lookup character and one
for the null terminator)
2) I declare a array, instead of needlessly allocating memory (and
forgetting to free it) as you did.
3) I assign the character in lookup to string[0] and the null terminator to
string[1], which is obviously what you were trying to do in your code.

john
 
F

Francis Glassborow

Julius Mong said:
Hi all, I'm doing this:

I assume you are using C++ because of the other newsgroup you cross
posted to, but other internals suggest that you may actually be
compiling as C.
// Test char code
wchar_t lookup[] = {0x8364, 0x5543, 0x3432, 0xabcd, 0xef01};
OK, very odd magic numbers but they should work OK but the compiler will
convert them to whatever wchar_t expects (and the type of wchar_t is
different between C and C++. In the former it is just a typedef for some
suitable integer type -- possibly unsigned short. In C++ it is a type of
its own.
for (int x=0; x<5; x++) {
wchar_t * string = (wchar_t*) malloc(sizeof(wchar_t));
so the very unwisely named variable (string) is a pointer to some memory
for storing an array of wchar_t but why?
string = (wchar_t*)lookup[1];
Now you take that pointer to malloced memory and try to forceably make
it use the value found in lookup[1] (which is a wchar_t value) as a
pointer to wchar_t. You are really confused. Please describe in words
exactly what you are trying to do and in which language. Even if you got
your code to work it sure would not be doing anything that you expect.
string[sizeof(wchar_t)] = '\0';
CComBSTR bstrTest = SysAllocString(string);
}

And the line string=lookup[...] gives me this error:

error C2440: '=' : cannot convert from 'unsigned short' to 'unsigned
short *'

Can someone tell me what I'm missing? I've tried casting it to (wchar_t*)
and it'd crash as expected... I'm stuck...

Thanks, Jules
 
R

Rolf Magnus

Julius said:
Hi all, I'm doing this:

// Test char code
wchar_t lookup[] = {0x8364, 0x5543, 0x3432, 0xabcd, 0xef01};
for (int x=0; x<5; x++) {
wchar_t * string = (wchar_t*) malloc(sizeof(wchar_t));

You allocate memory for exactly one wchar_t here. 'string' poitns to
that memory.
Btw: If you write in C, leave out the cast. It is not needed and can
obscure subtle errors. If your code is supposed to be C++ code, use new
instead of malloc. Again, you won't need the cast then.
string = (wchar_t*)lookup[1];

Now you overwrite the value of the pointer 'string', so the memory you
allocated above is lost forever. You produced a memory leak. Further,
you try to interpret the second entry of the lookup table as a pointer
and assign that to string. The resulting pointer is probably bogus.
Looks to me as if you actually wanted to copy the x'th (otherwise, what
would the loop be good for?) entry to the memory at the address that
string points to, which would be:

string[0] = lookup[x];
string[sizeof(wchar_t)] = '\0';

Why sizeof(wchar_t)? Did you want the second element? Then you have to
index it with [1], not with the size of the element type. Further, you
have only allocated enough memory for one wchar_t, so you try to write
beyond the memory for your string. That's a classic buffer overflow
situation. If you want to write two elements to your array, you have to
allocate memory for at least those two elements.

CComBSTR bstrTest = SysAllocString(string);
}

And the line string=lookup[...] gives me this error:

error C2440: '=' : cannot convert from 'unsigned short' to
'unsigned short *'

I assume that was without the above (wchar_t*) cast. And the compiler is
right. lookup[1] is of type wchar_t, but string is of type wchar_t*.
Can someone tell me what I'm missing? I've tried casting it to
(wchar_t*) and it'd crash as expected... I'm stuck...

Don't just cast if two types don't fit. Try to find out why they don't
fit. Also, if you are really programming in C++, you should better use
new/delete instead of malloc/free, wstring instead of wchar_t* and the
C++ casts (if you need to cast at all) instead of the C style cast.
Read a good C++ book to learn more about those things.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top