Thread: strcat()

  1. #1
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507

    strcat()

    Just bought the K&R "old testament" C book and was reading along. I came to one of the exercises though and became a bit stumped. It wants you to make an implementation of strcat using pointers. Is this possible to do without using sizeof/malloc ?

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Pass in an array that has more space than it needs.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    230
    you can use a very large array and hope it's large enough to hold both strings. Other than that I can't think of a way to do with without sizeof and malloc. Why don't you want to use them?

    EDIT: Sorry, I think I need to go drink some coffee :S like the rest said, you just need to append it, not sizeof or malloc needed
    Last edited by Abda92; 07-24-2008 at 11:26 AM.
    I might not be a pro, but I'm usually right

  4. #4
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Why do you need malloc()? strcat() doesn't allocate any memory, it just takes one string and appends it to the end of another.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    And besides, strcat is _supposed_ to trash memory if you don't pass it the right thing anyway, right?

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    What tabstop means is that if your implementation overwrites the end of the array when said array isn't big enough to have the given string stuck onto the end of it, then you're emulating strcat() precisely.

    BTW -- you can create a version with memory allocation by using realloc(), though of course you'll always have to pass a valid malloc()'d pointer to the function.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507
    Thanks

    That being said does anybody wanna chime in if my code is at all close to being the most efficient for this command?

    Code:
    #include <stdlib.h>
    
    void strcats(char * s, char * t);
    
    int main()
    {
        char one[500] = "Hi there ";
        char * two = "my name is Jeff";
        
        strcats(one, two);
        printf("%s", one);
    }
    //Copies string t onto end of string s
    void strcats(char * s, char * t)
    {
    
    for (; *s != '\0'; s++) //Find end of the string in S by pointing to that element
    {
    }
    for (; *t != '\0'; t++, s++)  //Loop to the end of T, concatenating char by char to S
    {
    *s = *t;
    }
    }

  8. #8
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by valaris View Post
    Thanks

    That being said does anybody wanna chime in if my code is at all close to being the most efficient for this command?

    Code:
    #include <stdlib.h>
    
    void strcats(char * s, char * t);
    
    int main()
    {
        char one[500] = "Hi there ";
        char * two = "my name is Jeff";
        
        strcats(one, two);
        printf("%s", one);
    }
    //Copies string t onto end of string s
    void strcats(char * s, char * t)
    {
    
    for (; *s != '\0'; s++) //Find end of the string in S by pointing to that element
    {
    }
    for (; *t != '\0'; t++, s++)  //Loop to the end of T, concatenating char by char to S
    {
    *s = *t;
    }
    }
    Looks good, except for the indenting (or lack thereof).

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    strcat() should add a \0 at the end as well.

  10. #10
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507
    I see Thanks. Btw anybody that doesn't have this book should get it, way better then regular vanilla C books. Learning 100&#37; more then other books i've read.

  11. #11
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Just one thing besides the '\0': the second parameter to strcat() is const, because it never gets modified. Assuming you want an exact replacement for strcat().

    [edit] If you want to be really picky . . . printf() is in stdio.h. [/edit]
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  12. #12
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507
    Sorry I usually forget to declare things const I guess because it is not required.

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, 05: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-14-2001, 12:09 AM