0

Here I want to merge two char arrays and put them into the first char array But when I cout the second one, it is missing the first letter. why?

#include <bits/stdc++.h>
using namespace std;
int main() {
    
    char s1[4] = "one";
    char s2[4] = "two" ;
    
    strcat(s1, s2);
    cout << s1 << endl; //onetwo
    
    cout  << s2; // wo

    return 0;
}
  • 2
    How long would the concatenated string be (including the null terminator)? How much space is in `s1`? – Nate Eldredge Mar 25 '23 at 22:02
  • 1
    Attempting to place a string with six characters into an array that's only big enough for three (excluding the zero terminator) is not going to work. – Sam Varshavchik Mar 25 '23 at 22:03
  • 4
    Just as a side note: [Why should I not #include ?](https://stackoverflow.com/q/31816095/12149471) – Andreas Wenzel Mar 25 '23 at 22:03
  • 1
    There is a very valid learning opportunity underlying this question. If it's going to be closed, hopefully you can at least point to a duplicate question. Are all cases of undefined behavior closed as "not reproducible"? Separately, the question I want to ask the OP is why they are using C strings instead of C++ strings. If it's homework, I hope their instructor is planning to explain that this coding style is from antiquity and there's no legitimate use of bare `strcat()` in modern C++. – Brent Bradburn Mar 25 '23 at 22:27
  • Why are you using `strcat` in C++ in the first place? And please, please, *stop* learning from whomever taught you to `#include `, as fast as possible. – Jesper Juhl Mar 26 '23 at 02:26
  • 2
    @BrentBradburn — it wasn’t closed as not reproducible. It was closed as a typo. Some people seem to think that if it would be a typo if they wrote it then it’s a typo if anyone wrote it. You’re absolutely right that it deserves more serious consideration than that. – Pete Becker Mar 26 '23 at 02:30

2 Answers2

1

When calling the function strcat, it is required that you pass as the first argument a pointer to a memory buffer that is large enough to store the concatenated string.

In your case, the concatented string has a length of 6 characters ("onetwo"), so the memory buffer must have a size of at least 7 characters (including the terminating null character). However, in your case, the memory buffer has a size of only 4 characters, which is insufficient. Therefore, your program is invoking undefined behavior, which means that anything can happen. This includes the behavior that you describe in the question.

To fix the problem, you must make the array s1 at least 7 characters long:

#include <iostream>
#include <cstring>

using namespace std;

int main() {
    
    char s1[7] = "one";
    char s2[4] = "two" ;
    
    strcat(s1, s2);

    cout << s1 << endl;    
    cout << s2 << endl;

    return 0;
}

This program has the desired output:

onetwo
two
Andreas Wenzel
  • 22,760
  • 4
  • 24
  • 39
0

In C++ you should use std::string for working with strings:

#include <iostream>
#include <string>

int main() {
    std::string s1 = "one";
    std::string s2 = "two";

    s1 += s1; // append s2 to s1

    std::cout << s1 << '\n';
    std::cout << s2 << '\n';
}
oneone
two
Benjamin Buch
  • 4,752
  • 7
  • 28
  • 51