Results 1 to 4 of 4

Thread: Type Casting

  1. #1

    Thread Starter
    Lively Member
    Join Date
    May 2000
    Posts
    123

    Question 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???

  2. #2
    Megatron
    Guest
    Use the itoa function.
    Code:
    char tmp[20];
    char* MyStr = itoa(10, tmp, 20);

  3. #3
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    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.
    Harry.

    "From one thing, know ten thousand things."

  4. #4
    Megatron
    Guest
    My code should read as:
    Code:
    char* MyStr = itoa(10, tmp, 10);  // base 10

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width