Thread: Please review my text concatenation try in C

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    3

    Please review my text concatenation try in C

    Hi

    I am trying to understand the strings in C so I tried to implement 3 different versions of text concatenation in C. I would love to hear



    -what would be the best approach(array vs pointers based on my try)

    -Any way to improve any of the approaches I tried?

    -In the "void *concText_p" function how can I "free" the newText pointer before returning it? Or what would be the best way to use malloc and concatenation in the same function as the way I am trying to do?


    1) uses array

    2) I try using pointers and returning a pointer

    3) I try using pointers and modify the original incoming variable.

    Code:
    #include <string.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    #define STRLEN 512
    char *concText(char *source1,  char *source2);
    char *concText_p(char *source1,  char *source2, size_t len);
    void *concText_p2(char *target, char *source1,  char *source2);
    
    void main() {
        char *source1="source1-0123456789";
        char *source2="source2-9876543210";
        char *result=(char *)malloc(512*sizeof(char)); //TODO remove the constant charlength here
            printf("(source1)-> %s  (source2)-> %s  len -> %d\n\n",source1,source2, (strlen(source1)+strlen((source2))));
    
        result=concText(source1,source2);
            printf("[MAIN] => Merged Text is %s  len is %d \n",result,strlen(result));
    
    //    result=concText_p(source1,source2,STRLEN);
    //        printf("[MAIN] => Merged Text is %s  len is %d",result,strlen(result));
    
    //    concText_p2(result, source1,source2);
    //        printf("[MAIN] => Merged Text is %s  len is %d \n",result,strlen(result));    z
    
    }
    
    char *concText(char *source1,  char *source2){
        int count=0;
        char *tmpSource1=source1;
        char *tmpSource2=source2;
    
        char newText[STRLEN];
    
        while(*(tmpSource1)!='\0'){
            newText[count]=*tmpSource1++;
            count++;
        }
    
        while(*(tmpSource2)!='\0'){
            newText[count]=*tmpSource2++;
            count++;
        }
            newText[count]='\0';
        return newText;
    }
    
    
    char *concText_p(char *source1,  char *source2, size_t len){
        int count=0;
        char *tmpSource1=source1;
        char *tmpSource2=source2;
    
        char *newText=malloc(len);
            if (newText==NULL){printf("It cannot allocate enough memory"); return EXIT_FAILURE;}
        while(*(tmpSource1)!='\0'){
            *newText++=*tmpSource1++;
            count++;
        }
        while(*(tmpSource2)!='\0'){
            *newText++=*tmpSource2++;
            count++;
        }
            *newText='\0';
        //Rollback to the starting adress of the text array
        while(count-->0){ newText--;}
    
        return newText;
    }
    
    void *concText_p2(char *target, char *source1,  char *source2){
        int count=0;
        char *tmpSource1=source1;
        char *tmpSource2=source2;
    
        char *newText=target;
    
        while(*(tmpSource1)!='\0'){
            *newText++=*tmpSource1++;
            count++;
        }
    
        while(*(tmpSource2)!='\0'){
            *newText++=*tmpSource2++;
            count++;
        }
            *newText='\0';
    }
    Last edited by sunnerjuk; 01-10-2012 at 01:58 PM. Reason: code cleanup

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Don't bother with the tmpSource1 and tmpSource2 variables. Just use the source1 and source2 parameters.

    You shouldn't be returning the address of local variable newText in concText. So even though it may SEEM to work, this method is no good.

    Don't "roll back" the newText var in concText_p. Instead, create a new variable, char *p = newText; after the malloc test and use p to walk through the array and pass back newText.

    This makes no sense:
    char *newText=malloc(STRLEN*len);
    You don't even need to pass len into that function. So get rid of it and just do this:
    char *newText=malloc(strlen(source1)+strlen(source2)+1) ;

    Almost forgot, you return EXIT_FAILURE from a function that's supposed to return a char*. You should just pass back NULL.
    Last edited by oogabooga; 01-10-2012 at 02:23 PM.

  3. #3
    Registered User
    Join Date
    Jan 2012
    Posts
    3
    oogabooga,

    Thanks for the great points, very helpful.

    I actually had this line in my oiriginal but I thought it might make more sense to pass a length, thou I now see that it is much more convenient


    char *newText=malloc(strlen(source1)+strlen(source2)+1) ;



    The rest of your points are well taken and thanks for suggestions.

  4. #4
    Registered User
    Join Date
    Jan 2012
    Posts
    3
    Quote Originally Posted by oogabooga View Post

    Don't "roll back" the newText var in concText_p. Instead, create a new variable, char *p = newText; after the malloc test and use p to walk through the array and pass back newText.
    .

    oogabooga,



    I have one more question about this malloc in a function. Do I need to "free" the allocated memory in the local function? If so how do I do it before the return function, I mean if I free it before return then I probably would not be able to return t? How would that work?





    thanks

  5. #5
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by sunnerjuk View Post
    I have one more question about this malloc in a function. Do I need to "free" the allocated memory in the local function? If so how do I do it before the return function, I mean if I free it before return then I probably would not be able to return t? How would that work?
    You are correct that you cannot free the memory in the function. The caller of the function is responsible for eventually freeing this memory.

  6. #6
    Registered User
    Join Date
    Jan 2012
    Posts
    3

    sunnerjuk

    Yes, you cannot free the memory before returning from the function (in your case) just free you
    result pointer in the last.

    Saurabh

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. concatenation
    By valthyx in forum C Programming
    Replies: 5
    Last Post: 01-17-2010, 08:22 AM
  2. Concatenation ?
    By koolguysj in forum C Programming
    Replies: 8
    Last Post: 04-15-2005, 04:26 PM
  3. Text Game Review
    By punkrockguy318 in forum Game Programming
    Replies: 16
    Last Post: 10-26-2003, 07:39 AM
  4. concatenation
    By rose2626 in forum C++ Programming
    Replies: 10
    Last Post: 04-25-2003, 01:27 PM
  5. concatenation
    By F*SH in forum C++ Programming
    Replies: 34
    Last Post: 11-13-2002, 06:47 PM