Thread: need help for a little function

  1. #1
    Registered User
    Join Date
    Sep 2020
    Posts
    6

    need help for a little function

    Hi, I'have been fighting with this function all night and I can't figure out what's wrong with it.

    here is my function:
    Code:
    char *cat(char *dest, char *src)
    {
            int j = strl(dest);
            int i;
    
            i = 0;
            while (src[i])
            {
                    dest[i + j] = src[i];
                    i++;
            }
            dest[i + j] = 0;
            return (dest + i +j);
    }
    
    
    The idea is simple, kind of like strlcat, we take as inputs a destination and a source .
    We paste the source at the first \0 of dest and then (this is were the problems start) we return the end of the pasted source so that we can keep on concatenating string.
    for some reason if I do a printf of the returned string inside the function everything is fine but if I try to printf it outside the function I get a seg fault.

    If you have any idea on what is wrong or can show me the way you solved this problem, please let me know.
    Any help is very much appriciated, thanks for your time.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    It looks like your issue is that instead of returning dest, you return (dest + i +j)

    Incidentally, I would rename j to something like dest_len. j makes it look like an index whereas you're using it to store the length of dest.

    There's also no harm in declaring src to be a const char * instead since its content is never modified, and there is indeed no reason to modify it.

    You should also be aware that what you're implementing is akin to the standard strcat rather than the standard library extension strlcat, the latter having a parameter for the destination array size, hence reducing the chance of a mistake resulting in a buffer overflow vulnerability, but at the cost of unnecessary checks when it has been conclusively determined that the result can fully fit in the destination array.
    Last edited by laserlight; 09-21-2020 at 03:49 PM.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 02-19-2020, 05:50 AM
  2. Function Prototype, Function Call, and Function definition
    By dmcarpenter in forum C Programming
    Replies: 9
    Last Post: 04-09-2013, 03:29 AM
  3. Replies: 13
    Last Post: 03-20-2012, 08:29 AM
  4. Print function: sending a function.. through a function?
    By scarlet00014 in forum C Programming
    Replies: 3
    Last Post: 11-05-2008, 05:03 PM
  5. Replies: 9
    Last Post: 01-02-2007, 04:22 PM

Tags for this Thread