Thread: Custom strcat() function.

  1. #1
    Registered User
    Join Date
    May 2017
    Posts
    31

    Custom strcat() function.

    hello;

    i am trying to write my own version of strcat() that uses pointers. i dont know why its not working; can someone take a look at this piece of code?

    thanks in advance

    Code:
    #include <stdio.h>
    
    char *my_strcat(char *a, char *s_a);
    
    int main(void){
        char x1[30] = "a";
        char x2[10] = "boy";
        my_strcat(x1, x2);
    
        printf("%s", x1 );
    
    }
    
    char *my_strcat(char *a, char *b){
    
        while (*a++ != '\0');
    
        while(*b != '\0'){
            *a++ = *b++;
        }
        *a = '\0';
        return a;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    You advance a too far.
    Code:
    $gcc -g main.c
    $ gdb -q a.out
    Reading symbols from a.out...done.
    (gdb) list
    1	#include <stdio.h>
    2	 
    3	char *my_strcat(char *a, char *s_a);
    4	 
    5	int main(void){
    6	    char x1[30] = "a";
    7	    char x2[10] = "boy";
    8	    my_strcat(x1, x2);
    9	 
    10	    printf("%s", x1 );
    (gdb) b 10
    Breakpoint 1 at 0x4005f3: file main.c, line 10.
    (gdb) run
    Starting program: /home/sc/Documents/a.out 
    
    Breakpoint 1, main () at main.c:10
    10	    printf("%s", x1 );
    (gdb) p x1
    $1 = "a\000boy", '\000' <repeats 24 times>
    (gdb)
    You append the string OK, but you appended it in the wrong place.

    It's a good time to familiarise yourself with a debugger to help you figure out such things for yourself.
    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.

  3. #3
    Registered User
    Join Date
    May 2017
    Posts
    31
    Quote Originally Posted by Salem View Post
    You advance a too far.
    Code:
    $gcc -g main.c
    $ gdb -q a.out
    Reading symbols from a.out...done.
    (gdb) list
    1    #include <stdio.h>
    2     
    3    char *my_strcat(char *a, char *s_a);
    4     
    5    int main(void){
    6        char x1[30] = "a";
    7        char x2[10] = "boy";
    8        my_strcat(x1, x2);
    9     
    10        printf("%s", x1 );
    (gdb) b 10
    Breakpoint 1 at 0x4005f3: file main.c, line 10.
    (gdb) run
    Starting program: /home/sc/Documents/a.out 
    
    Breakpoint 1, main () at main.c:10
    10        printf("%s", x1 );
    (gdb) p x1
    $1 = "a\000boy", '\000' <repeats 24 times>
    (gdb)
    You append the string OK, but you appended it in the wrong place.

    It's a good time to familiarise yourself with a debugger to help you figure out such things for yourself.
    Thanks for your reply. i have a question though. which debugger do you think is appropriate for someone who has zero
    experience in debugging. i am a using a laptop running windows

  4. #4
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,106
    Quote Originally Posted by thagulf2017 View Post
    Thanks for your reply. i have a question though. which debugger do you think is appropriate for someone who has zero
    experience in debugging. i am a using a laptop running windows
    If your compiler comes with a debugger, then that is the one you should use. If not, then maybe install the gcc compiler and debugger.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Note that the return value of strcat is a pointer to the (first character of the) resulting string, i.e., it returns a pointer equal to the first parameter's value. Since you modify the parameter a before returning it, my_strcat therefore differs from strcat in this respect.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    May 2017
    Posts
    31
    thank you all for replies, it works now

  7. #7
    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