Click to See Complete Forum and Search --> : Type Casting
highlife
Apr 26th, 2001, 01:37 PM
okay im trying to typecast an int to a LPCTSTR as follows:
int betamount = 5;
SetDlgItemText(ID_BET,(LPCTSTR)betamount); // requires LPCTSR as a parameter
this will compile fine but it gives gives an application error of referencing illegal memory when i run the app.
what am I doing wrong???
Megatron
Apr 26th, 2001, 02:43 PM
Use the itoa function.
char tmp[20];
char* MyStr = itoa(10, tmp, 20);
HarryW
Apr 26th, 2001, 03:22 PM
The reason it doesn't work is because a string is an array of characters, which as far as the compiler is concerned is identified by the address of the first element. If you pass in, say, 5 to a function which is expecting a string, then it will use 5 as the address of the string. So you are derefererencing an invalid pointer. Also, the number 5 is not the same as the character '5'. The itoa() function (short for int to ascii) converts a number to a string representation of that number, in a base (also called a radix) that you specify. Usually you will want it in decimal, so the radix is 10.
Megatron
Apr 26th, 2001, 05:02 PM
My code should read as:
char* MyStr = itoa(10, tmp, 10); // base 10
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.