473,325 Members | 2,860 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

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

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


Jul 22 '05 #1
3 2959

"Julius Mong" <jx****@hotmail.com> wrote in message
news:c2*******************@news.demon.co.uk...
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

Jul 22 '05 #2
In message <c2*******************@news.demon.co.uk>, Julius Mong
<jx****@hotmail.com> writes
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


--
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
Jul 22 '05 #3
Julius Mong wrote:
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.

Jul 22 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: John Collyer | last post by:
Hi, In assembly language you can use a lookup table to call functions. 1. Lookup function address in table 2. Call the function Like: CALL FUNCTION
2
by: Isak Dinesen | last post by:
Being relatively new to c++, I've recently discovered a few things about the behavior of new with respect to wchar_t, ZeroMemory and delete. I can't seem to find documentation describing the...
23
by: Steven T. Hatton | last post by:
This is one of the first obstacles I encountered when getting started with C++. I found that everybody had their own idea of what a string is. There was std::string, QString, xercesc::XMLString,...
0
by: fswfsw | last post by:
-------------------------------------------------------------------------------- Hi guys, I am using Window form application in Visual studio 2005 to accomplish a serial communication. Now...
13
by: paul.joseph.davis | last post by:
Hi, I've just had my first encounter with two-phase lookup and I'm scratching my head a bit. The idea behind two phase look up is pretty easy to understand, but I have a case that fails to...
4
by: interec | last post by:
Hi Folks, I am writing a c++ program on redhat linux using main(int argc, wchar_t *argv). $LANG on console is set to "en_US.UTF-8". g++ compiler version is 3.4.6. Q1. what is the encoding of...
2
by: anson | last post by:
this code is an example code of TPOP, i type them and can be compiled... but when give some input , it getting segment fault .... i observed that when the build() initial the word table ... it...
3
by: john | last post by:
As far as I know there is only the type wchar_t. However my compiler compiles both "signed wchar_t" and "unsigned wchar_t". Are there both signed and unsigned wchar_t types?
5
by: Samant.Trupti | last post by:
Hi, There is one thing I am cofused about.... If I have a declareation say char str; Now if I want to change it to wchar so do I have to change it like .... wchar_t str; or double the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.