 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Julius Mong Guest
|
Posted: Sat Mar 06, 2004 12:45 pm Post subject: Trying to get wchar_t... from a lookup array but type error. |
|
|
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)] = ' ';
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
|
|
| Back to top |
|
 |
John Harrison Guest
|
Posted: Sat Mar 06, 2004 1:13 pm Post subject: Re: Trying to get wchar_t... from a lookup array but type er |
|
|
"Julius Mong" <jxm96c (AT) hotmail (DOT) com> wrote
| Quote: | 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)] = ' ';
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.
| Quote: | 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] = ' ';
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
|
|
| Back to top |
|
 |
Francis Glassborow Guest
|
Posted: Sat Mar 06, 2004 1:27 pm Post subject: Re: Trying to get wchar_t... from a lookup array but type er |
|
|
In message <c2ch4h$sq2$1$8300dec7 (AT) news (DOT) demon.co.uk>, Julius Mong
<jxm96c (AT) hotmail (DOT) com> writes
| Quote: | 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.
| Quote: |
// 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.
| Quote: | 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?
| Quote: | 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.
| Quote: | string[sizeof(wchar_t)] = ' ';
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
|
--
Francis Glassborow ACCU
Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
For project ideas and contributions: http://www.spellen.org/youcandoit/projects
|
|
| Back to top |
|
 |
Rolf Magnus Guest
|
Posted: Sat Mar 06, 2004 7:27 pm Post subject: Re: Trying to get wchar_t... from a lookup array but type er |
|
|
Julius Mong wrote:
| Quote: | 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.
| Quote: | 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];
| Quote: | string[sizeof(wchar_t)] = ' ';
|
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.
| Quote: | 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*.
| Quote: | 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.
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|