strcat() in c and append in c++

I declared char s[100] as a character array and then in the later part of the program i took s as an input string and then used strcat(s,s) ,then it showed "Runtime Error"
But when in c++ , i declared s as a string and used s.append(s), then it worked correctly and both these functions perform the same function , so why is there an error in c but not in c++??

Note, I will be reffering to character arrays as strings.

strcat() will overwrite everything ( starting from the null character in the first string ) in the first string until the null character '\0' in the second string is reached. But, when you pass the same string as both the parameters, then it will start overwriting the first string and overwrites the null character, and at the same time, the second string obtains the same changes. So, the null character wil be overwritten and there will be nothing left to stop the execution of strcat().

Let us look at it. This is a string

s t r i n g \0
^            ^
st           en

st is the start and en is the position to be overwritten. Now let us look how strcat() works

s t r i n g s
  ^           ^
  st          en

s t r i n g s t
    ^           ^
    st          en

s t r i n g s t r
      ^           ^
      st          en

s t r i n g s t r i
        ^           ^
        st          en

s t r i n g s t r i n
          ^           ^
          st          en

s t r i n g s t r i n g
            ^           ^
            st          en

s t r i n g s t r i n g s
              ^           ^
              st          en

As you can see, it will keep on appending the string without an end as there is no longer a null character.

Since there is no null character to tell the function to stop, it just keeps on appending it. .So, once the string exceeds the length you specified, it will try to access unsafe memory which should not be accessed and you will get a SEGMENTATION FAULT.

This is what is causing the problem.

But this is not how append() in c++ works. So you don’t get an error there.

thanx a lot!
cheers