Can I concatenate a char[] and int?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • BurnTard
    New Member
    • May 2007
    • 52

    Can I concatenate a char[] and int?

    I'm making a program that is supposed to change certain input and leave most unchanged, and then spit out an output. I decided I should store the output in a single char[] before writing it out, so I could build on the program later, however doing this I ran into an issue. Anyway, the main.c is attached as a txt.

    Changing all the stuff seems to work fine, it's just that adding the normal characters isn't working. I get warnings, and the program dies immediately. How should I go about turning the int into a char so I can add it with strcat?
    Attached Files
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    You problem is that strcat (along with all the other string functions) works on a zero terminated array of characters.

    Your arrays n, t and bs are fine because they conform to this criteria. However m is not an array of char, it is a char. I am surprised your program compiles without warnings or errors but

    strcat(output,m );

    where m has type char is never going to work. You need to create a proper C string out of c before calling strcat.

    Comment

    • gpraghuram
      Recognized Expert Top Contributor
      • Mar 2007
      • 1275

      #3
      Try to use sprintf to join the int with the char.
      Code:
      sprintf(final,"%s%d",original,integer);
      Raghu

      Comment

      Working...