Thread: concatenate int onto char

  1. #1
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300

    concatenate int onto char

    How can I convert an integer to a string for inclusion in another string? eg, here's a try with typecasting that doesn't work:
    Code:
    int main () {
            int x=5;
            char *str=(char*)x;
            printf("%s", str);
    }
    Compiles fine but segfaults.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, it would crash. You are trying to read a string from locaiton 5 in memory.

    To concatenate a number to a string, you need to convert the internal (binary) representation to a string, and then concatenate the string. An easy way to do that would be to use sprintf() to "print" the number into a string buffer - you could even do something like this:
    Code:
       char buffer[100];
       int n = 12;
       ...
       sprintf(buffer, "Test%03d.txt", n);
       ...
    Buffer would in this case contain a string "Test012.txt"

    --
    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.

  3. #3
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    You may want to thank about the following:

    Code:
    #include <stdlib.h>
    
    /*Prototype */
    char *itoa(int value, char *string, int radix);

  4. #4
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Code:
    itoa( num, strOut, 10 );
    which is compiler specific, or
    Code:
    sprintf( strOut, "%d", num );
    which is standard C.

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    ahh, now that was nice and simple (sprintf)...thanx
    Last edited by MK27; 08-06-2008 at 09:59 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

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. Replies: 14
    Last Post: 06-28-2006, 01:58 AM
  3. Working with random like dice
    By SebastionV3 in forum C++ Programming
    Replies: 10
    Last Post: 05-26-2006, 09:16 PM
  4. Game Won't Compile
    By jothesmo in forum C++ Programming
    Replies: 2
    Last Post: 04-01-2006, 04:24 PM
  5. 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

Tags for this Thread