Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8868669
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T17:19:21+00:00 2026-06-14T17:19:21+00:00

This is the strcat function that I have implemented, but I get a segmentation

  • 0

This is the strcat function that I have implemented, but I get a segmentation fault when I go to the line *dst++ = *src++;. I have incremented src till '\0' as I want to append the next string starting from there. Can you please tell me why it gives segmentation fault? Am I doing any logical mistake by doing *dst++ = *src++;?

char *strconcat(char *dst, char *src)
{
    char *fdst;
    fdst = dst;
    if (dst == '\0' || src == '\0')
    return fdst;

    while (*dst != '\0')
        dst++;
    while (*src != '\0')
        *dst++ = *src++;

    return fdst;
}

Hey i went through many solution given below and i made following changes but still i get segmentation problem when i start concatenate two strings, here is my entire code

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

char *strconcat(char *dst, const char *src);
void main()
{
    char *str1 = "Stack";
    char *str2 = "overflow";
    printf("MY CONCAT:%s\n",strconcat(str1, str2));
    printf("INBUILT CONCAT:%s\n",strcat(str1, str2));
}

char *strconcat(char *dst, const char *src)
{
    char *fdst;
    int dst_len = 0, src_len = 0;

    dst_len = strlen(dst);
    src_len = strlen(src);
    fdst = (char *) malloc(dst_len + src_len + 1);

    fdst = dst;
    if (src == NULL)
        return fdst;

    while(*dst)
    {
       dst++;
       fdst++;
    }
    while(*src)
       *fdst++ = *src++;
       *fdst = '\0';

    return fdst;
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T17:19:22+00:00Added an answer on June 14, 2026 at 5:19 pm

    Multiple problems here:

    PROBLEM – I

    The memory allocated with malloc

        fdst = (char *) malloc(dst_len + src_len + 1);
    

    is lost as some lines later you are doing this:

        fdst = dst;
    

    replacing the address returned by malloc in ‘fdst’ with the address of the target string..!! Hope you fix it on your own, its darn simple.

    PROBLEM – II

    After fixing that above problem, you’ll have to fix this:

    while(*dst)
    {
       dst++;
       fdst++;
    }
    

    don’t only increment, you’ll also have to copy your characters from dst to fdst as this will be your concatenated string.

    PROBLEM – III

    finally, you are doing this in the end..!!

    return fdst;
    

    you realize the mistake there right? Hope you can take care of that [ Hint: save the starting address and return it in the end, not the incremented pointer 😉 ]

    Note: Not an optimised solution but, fixes your code.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is that safe to do something like this: char* charArray = new char[10]; strcat(charArray,
I have a function irc_sendline that can be called like printf can irc_sendline(s, A
I have an exercise where I need to write a wrapper function for strcat.
I have an archive file that looks like this: !<arch> file1.txt/ 1350248044 45503 13036
I have this function, the input is the var cmd for example a dmesg
I have a function that converts a relative path (e.g. log\crash.txt) to the full
I have created a function that creates a dynamic string array with dynamic string
Okay - yes, this is homework, but it isn't mine. I have a friend
here i have one function which is call many times in one application this
This is a function that i've written: uint32_t file_list(char *path, char ***ls){ DIR *dp;

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.