Thread: copy a string in the end of another string!?

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    30

    copy a string in the end of another string!?

    Hi!
    I have two strings:
    char str_1[] = "First String ";
    char str_2[] = "Second String";

    how do I copy the second string at the end of first string, so the result would be:
    "First String Second String"
    Thank you!

  2. #2
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    By the way you have done it, you cannot. You'll have to have a third var that is large enough to hold both str_1 and str_2 with the '\0' at the end. Then, however, you could use strcat() or sprintf().

    EDIT: Or, you could make str_1[50] so that it would give you enough space.

  3. #3
    Registered User
    Join Date
    Mar 2006
    Posts
    1
    Kennedy is correct. I think you could also try making them both pointers, or at least make the first one a pointer.

    char * str_1 = "First String ";
    char * str_2 = "Second String";

    That way you don't have to worry about how big str_1 is. Isn't there a string function strcat() that will do what you want? I believe that takes two strings as arguments, and appends one to the end of the other. I could be wrong though.

  4. #4
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Nope, that still won't work. You gotta have more memory. You'll _HAVE_ to make str_1 bigger some way, whether by malloc() (using a char *) or statically defining it as a bigger chunk of memory.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > That way you don't have to worry about how big str_1 is
    Not only do your pointers point to strings in read-only memory (on any decent 32 bit platform anyway, so even trying to change them is fatal), you ALWAYS have to worry about how much memory you have to play with.

    There is no such thing as a pointer to an infinite amount of memory.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    well this will do what u want. Had a sample code before. This is just a sample code

    Code:
    str1 = malloc(sizeof(char) * 10);
        str2 = malloc(sizeof(char) * 10);
        
        strcpy(str1,"hello");
        strcpy(str2,"world");
        
        str1 = realloc( str1 , sizeof(char) * strlen(str2) );
                
        strcat(str1,str2);
    dont forget to free the str1 and str2

    ssharish2005

  7. #7
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Nope, this won't work either. You have to have a memory location big enough to handle BOTH strings. i.e. (to modify your code)
    Code:
    str1 = realloc(str1, sizeof(char) * (strlen(str2) + strlen(str1) + 1));

  8. #8
    Registered User
    Join Date
    Nov 2006
    Posts
    42
    Quote Originally Posted by Kennedy
    Code:
    str1 = realloc(str1, sizeof(char) * (strlen(str2) + strlen(str1) + 1));
    Do you have to write 'sizeof(char)'? Just wondering. Since char is size 1 (or maybe not everywhere?) so it's like multiplication by 1... or is it a good programming style? I have no idea, anybody could explain this a bit?

  9. #9
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    It's just something Kennedy likes to do. You don't have to multiply by sizeof(char) as the char type will always be the smallest accessible byte.
    Code:
    char *temp = realloc( str1, strlen(str1) + strlen(str2) + 1 );
    Will always ask for at least enough memory. Once you start using dynamic memory for things other than strings then you have to start multiplying sizes.

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > Do you have to write 'sizeof(char)'?
    Not if it's a char

    But where you had
    str1 = malloc(sizeof(char) * 10);

    I would have
    str1 = malloc( sizeof(*str1) * 10);
    which always gives the right answer regardless of what type of pointer str1 is.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem building Quake source
    By Silvercord in forum Game Programming
    Replies: 16
    Last Post: 07-11-2010, 09:13 AM
  2. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  3. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  5. Next Question...
    By Azmeos in forum C++ Programming
    Replies: 3
    Last Post: 06-06-2003, 02:40 PM