You did not initialize dest
char dest[1020] = "";
You just got lucky it happened that the 6th (random) value in dest was 0 . If it were the 1000th character, your return value would be much longer. If it is greater than 1024, you will get undefined behavior.
Strings as char arrays must be separated by the character 0 . Otherwise, it is not reported where they end. You can also say that the string ends with a null character, explicitly setting it to 0;
char dest[1020]; dest[0] = 0;
Or you can initialize the whole array with 0
char dest[1024] = {};
And since your question is marked by C++ , I cannot help but note that in C ++ we use std::string , which saves you a big headache. The + operator can be used to concatenate two std::string s
Armen Tsirunyan
source share