-
Type Casting
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???
-
Use the itoa function.
Code:
char tmp[20];
char* MyStr = itoa(10, tmp, 20);
-
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.
-
My code should read as:
Code:
char* MyStr = itoa(10, tmp, 10); // base 10