Thread: int to char*

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    1

    int to char*

    I'm looking for a way to convert an int to a char*. For instance, I have the number 3, and I want to convert that into a char* "3". I tried this but it doesnt work:

    Code:
    int i = 4;
    char* c = (char*)i;

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Well, the code you have posted does what you told it to do - which is to convert an integer to a char pointer. c will be a pointer pointing to address 4.

    But what you conceptually seem to want to do is to convert an integer to a string, which is a different thing. I tend to use sprintf() for that purpose. The language C in itself has no functionality to do what you want, you DO need to call a function of some sort. itoa() is another function that CAN do this, but it's not a standard function.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. memory leak
    By aruna1 in forum C++ Programming
    Replies: 3
    Last Post: 08-17-2008, 10:28 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. getting a headache
    By sreetvert83 in forum C++ Programming
    Replies: 41
    Last Post: 09-30-2005, 05:20 AM
  4. Quack! It doesn't work! >.<
    By *Michelle* in forum C++ Programming
    Replies: 8
    Last Post: 03-02-2003, 12:26 AM
  5. easy if you know how to use functions...
    By Unregistered in forum C Programming
    Replies: 7
    Last Post: 01-31-2002, 07:34 AM