Thread: How to transfer int to char *

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    1

    Lightbulb How to transfer int to char *

    Hello, everybody,
    In my code I need to transfer int to char *;

    Ex:

    int i=2345;

    char *ptr,str[4];
    ptr=str;

    ptr=(char *)&i; /* ;-((( doesn't work*/

    printf("ptr is %d",*ptr); /*I would like to see it on screen later*/

    Waiting your reply,
    and thanks for any help

  2. #2
    julie lexx... btq's Avatar
    Join Date
    Jun 2002
    Posts
    161
    try this:

    Code:
    int main()
    {
    int i=2345;
    
    char *ptr,str[4];
    ptr=str;
    ptr=(char *)i;
    printf("ptr is %d",ptr); 
    
    return 0;
    }
    /btq
    ...viewlexx - julie lexx

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Code:
    int i=2345;
    
    char *ptr;
    
    ptr=(char *)&i; /* ;-((( doesn't work*/
    
    printf("ptr is %d\n",*(int *) ptr); /*I would like to see it on screen later*/
    Or if you want to convert an int into a string, one way is sprintf().
    Code:
    int i=2345;
    char str[12];
    
    sprintf(str,"%d",i);
    printf("str is %s\n",str);

  4. #4
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    of course you can do that, but if your goal is to print pointer addresses with printf() you should use the %p conversion specifier.

    edit: on second thought, it is unclear exactly what you want to accomplish here, so only do the above if that is what you want to do
    Last edited by moi; 10-15-2002 at 02:31 PM.
    hello, internet!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can some one please tell me the cause of the error ?
    By broli86 in forum C Programming
    Replies: 8
    Last Post: 06-26-2008, 08:36 PM
  2. Working with random like dice
    By SebastionV3 in forum C++ Programming
    Replies: 10
    Last Post: 05-26-2006, 09:16 PM
  3. Game Won't Compile
    By jothesmo in forum C++ Programming
    Replies: 2
    Last Post: 04-01-2006, 04:24 PM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 05:26 PM