Click here to Skip to main content
15,170,741 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
I am trying this code ibut it is showing segmentation fault at run time.
But when I am using char s[] and char t[] instead of char *s , char *t in main(), the problem gets rectified.I dont know why ?

void my_strcat ( char *s , char *t)
 {int i , j;
  for( i=0;s[i] !='\0'; i++)
   for( j=0; t[j] !='\0';j++)


   cout<<"concatenated string is"<<endl<<s;

int main ()

   char *s="lion";
   char  *t="california";
   my_strcat(s , t );

   return 0;

ARopo is right. You must provide enough space in your buffer to put the extra characters.
Use this
char s[100] = "lion";

And it is always a good practice to give the length of your buffer. This is called defensive programming:

void my_strcat (char *s, int maxLength, const char *t)
    if (maxLength <= 0)
    int i, j;
    //always make sure we don't go too far by testing the maxLength
    for (i = 0; i < maxLength && s[i] != '\0'; i++);

    for (j = 0; i + j < maxLength && t[j] != '\0'; j++)
       s[i + j] = t[j];

    if (i + j < maxLength)
       s[i + j] = '\0';
       s[maxLength - 1] = '\0';

    cout << "concatenated string is " << endl << s;

int main ()
    char s[100] = "lion";
    char *t = "california";
    my_strcat(s, 100, t);
    return 0;
Finally! Nobody made a reasonably good answer before you did. (So many people are attracted by easy Questions, usually...). My 5.
Olivier Levrey 10-Mar-11 4:01am
Thank you SA.
Olivier Levrey 10-Mar-11 4:12am
(Nothing to do with this thread) I tried to help someone but couldn't succeed. Maybe you will have more ideas:
I saw this Question, decided to skip it. Boring stuff, needs some time and concentration, I don't think it needs any ideas, just that. I kinda a bit tired of doing such stuff and exactly for those manufacturer's code, including video cards, and often everyone on the teams needs help on such thing. The problem is that manufacturer's error is involved. Did you noticed, hardware manufacturer are most illiterate in programming, especially those making professional-grade hardware.

So, I don't know... I'll take a look. It needs some time...
You can't copy to s this is assigned a string constant so has not memory allocated for the characters you are adding , try this

char s[20];

my_strcat(s , t );

also you might want to change your function to
void my_strcat ( char *s , const char *t)
Olivier Levrey 9-Mar-11 5:46am
Correct. My 5. Good point about const.
But the string initialization could be better.
you must alloc memory for resulting string ...
Olivier Levrey 9-Mar-11 5:47am
Voted 3. It is obvious that OP is a beginner and you should explain more. Besides OP doesn't need to alloc memory: providing a larger buffer is enough.
Piccadilly Yum Yum 9-Mar-11 6:43am
TY, Oli, vote 2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900