Thread: Custom strcat() function.

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    May 2017
    Posts
    31
    thank you all for replies, it works now

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Minor thoughts and nitpicks.

    > char *my_strcat(char *a, char *b)

    Granted this is a custom strcat, but the signature doesn't match strcat. In particular, the second argument does not need to be modifiable and best practice would be to make it const to avoid inadvertent changes.

    > while (*a++ != '\0');

    I'm not a huge fan of directly modifying a parameter, especially when the interface of the function returns the parameter unchanged. Also, while I'll contradict myself immediately, one-liner statements with a lot happening are more difficult to wrap your head around as well as debug.

    > *a = '\0';

    This is unnecessary if you copy it in your loop.

    Here's an example of a standard-conforming strcat for comparison purposes:
    Code:
    char *strcat(char * restrict dst, const char * restrict src)
    {
        char *p = dst;
    
        /* Find the terminating null character in dst */
        while (*p)
        {
            ++p;
        }
    
        /* Copy from the null to append src */
        while (*p++ = *src++)
            ;
    
        return dst;
    }
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How does the function strcat use?
    By zcrself in forum C Programming
    Replies: 10
    Last Post: 08-14-2009, 12:53 AM
  2. Problem with strcat() function
    By sureshkumarct in forum C Programming
    Replies: 6
    Last Post: 01-03-2007, 08:05 PM
  3. need help with strcat function
    By zell in forum C Programming
    Replies: 28
    Last Post: 02-20-2005, 01:38 PM
  4. my strcat function
    By kurz7 in forum C Programming
    Replies: 2
    Last Post: 05-06-2003, 03:00 AM
  5. strcat function
    By Peachy in forum C Programming
    Replies: 1
    Last Post: 09-23-2001, 07:04 PM

Tags for this Thread