473,230 Members | 1,549 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,230 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 2954

"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: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
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: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...

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.