Appending to a C string

This is a discussion on Appending to a C string within the C Programming forums, part of the General Programming Boards category; Say I want to append a string onto the end of a string received in the form of a command ...

  1. #1
    Registered User Sharke's Avatar
    Join Date
    Jun 2008
    Location
    NYC
    Posts
    303

    Appending to a C string

    Say I want to append a string onto the end of a string received in the form of a command line argument and assigned to a pointer-to-char. How do I do that? Can I use strcat(), or is that not a safe thing to do?

  2. #2
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,160
    Quote Originally Posted by Sharke View Post
    Say I want to append a string onto the end of a string received in the form of a command line argument and assigned to a pointer-to-char. How do I do that? Can I use strcat(), or is that not a safe thing to do?
    strcat() will do that, but you have to ensure that the buffer you use to hold the result is large enough. This might mean calling malloc()
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  3. #3
    Registered User Sharke's Avatar
    Join Date
    Jun 2008
    Location
    NYC
    Posts
    303
    I see. I'm using malloc() now and everything seems fine.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Please check my C++
    By csonx_p in forum C++ Programming
    Replies: 263
    Last Post: 07-24-2008, 09:20 AM
  2. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  3. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM
  4. problems with overloaded '+' again
    By Brain Cell in forum C++ Programming
    Replies: 9
    Last Post: 04-14-2005, 05:13 PM
  5. Again Character Count, Word Count and String Search
    By client in forum C Programming
    Replies: 2
    Last Post: 05-09-2002, 11:40 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21