Thread: Adding two strings into one string

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    15

    Question Adding two strings into one string

    Hello,
    So I guess I have a problem with understanding the whole string issue in C, since i've been working on this little piece of code for a while now, trying to achieve what I want.

    Here is the code. How do I add two strings (head and body) into one string and return it to the caller?

    Code:
    char* compose(int type){
    	char *response, *head, *body;
    	
    	switch (type){
    		case	1:{
    			head=f1();
    			body=f2();
    			break;
    		}case 2:{
                            ...
    		}
                     ...
    	}
    	//Some code to do here - adding head and body into response
            return response;
    }
    The thing is that I dont know the size of the string that respose will contain (well, I know, but I try to do it differently), and so I can't declare it as a char array, and so I can't use strcat(). If I do declare it as char array, then I cant return it, because it is a local variable.
    Anyone can give me a hand here?
    Thanks in advance!

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    lookup malloc. malloc - C++ Reference

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    15
    Quote Originally Posted by stahta01 View Post
    lookup malloc. malloc - C++ Reference

    Tim S.
    I don't want to use malloc(). As far as I understand, I should use malloc() when only if I need to allocate memory dynamically. Here the size of the strings is fixed.
    Is there a way to do without malloc()?

  4. #4
    Registered User
    Join Date
    Nov 2011
    Posts
    52
    You can pass an char array from the calling function. Then store the response in there so you don't need to use malloc.

    So it will be like this. Just make sure that head and body will fit in response.

    Code:
    void compose(int type, char *response){
    
        char *head, *body;
    
         
        switch (type){
            case    1:{
                head=f1();
                body=f2();
                break;
            }case 2:{
                            ...
            }
                     ...
        }
        //Some code to do here - adding head and body into response
            return response;
    }

  5. #5
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    These two statements of yours seem to be at odds:
    The thing is that I dont know the size of the string that respose will contain
    I don't want to use malloc(). As far as I understand, I should use malloc() when only if I need to allocate memory dynamically. Here the size of the strings is fixed.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. please help! adding to strings
    By coni in forum C Programming
    Replies: 1
    Last Post: 10-02-2009, 09:16 PM
  2. adding strings?
    By Led4urhead123 in forum C++ Programming
    Replies: 2
    Last Post: 07-10-2008, 03:13 PM
  3. Adding two strings to make a wchar_t
    By ElWhapo in forum C++ Programming
    Replies: 4
    Last Post: 01-23-2005, 11:36 PM
  4. adding onto strings
    By Granger9 in forum C Programming
    Replies: 6
    Last Post: 09-11-2002, 01:06 PM
  5. Adding to strings..
    By SyntaxBubble in forum Windows Programming
    Replies: 1
    Last Post: 10-25-2001, 04:22 PM