How to use strcat

This is a discussion on How to use strcat within the C++ Programming forums, part of the General Programming Boards category; Problem: I am not able to pass two parameters, concatenate them in dynamically allocated memory, delete the memory and return ...

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    2

    How to use strcat

    Problem: I am not able to pass two parameters, concatenate them in dynamically allocated memory, delete the memory and return the concatenated value. It seems like this should be straight forward, but I am running into all types of issues.

    Code:

    Code:
    TestString TestString::concat(const TestString &value) const
    {
    
        //Length
        char *str = new char[(strlen(str) + strlen(value.str) + 1)];
    
        //strcat
        strcat(str, value.str);
    
        //delete ??
        //somehow i need to delete before I return the value;
        delete [] str;
        //This most likely to early since I haven't returned anything yet
    
        //return
        return str;
    }
    Last edited by theprophet; 04-30-2005 at 09:47 AM.

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    I am not able to pass two parameters, concatenate them in dynamically allocated memory, delete the memory and return the concatenated value.
    If you delete the memory, then the string you created in that memory is destroyed.

  3. #3
    Registered User
    Join Date
    Apr 2005
    Posts
    2
    Thanks to those that looked but I figured it out. I will post my solution in a while.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. An interesting question about strcat
    By meili100 in forum C++ Programming
    Replies: 3
    Last Post: 07-07-2009, 12:59 PM
  2. strcat the inside of a structure
    By ebullient in forum C Programming
    Replies: 3
    Last Post: 12-09-2005, 04:58 PM
  3. strcat and a char
    By jjacobweston in forum C++ Programming
    Replies: 2
    Last Post: 05-09-2005, 04:10 PM
  4. strcat makes program crash
    By imoy in forum C++ Programming
    Replies: 4
    Last Post: 05-09-2002, 02:41 PM
  5. Removing 'strcat' ed string.
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 11-13-2001, 11:09 PM

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