"Hello"랑 "World!"가 있을 때 이 문자열들을 합치고 싶은데

"Hello"랑 "Hello"를 합치면 "HelloHelloH"가 돼있고 이걸 strcpy하면 "HelloHello\0UU\0\0h..." 이런식으로 되고

"Hello"랑 "World!"를 합치면 "HelloWorld!"는 정상적으로 되는데 strcpy하면 "HelloWorld!\0UU...." 처럼 됨

뒤에 H, h, U, �  같은 것들은 뜬금없이 왜 튀어나오는지 이유를 모르겠음



아래는 코드


#define MAX_LEN 25
#include <iostream>
#include <cstring>
using namespace std;

int main()
{
    cout << "============temp1 = strcat(Hello, Hello)==============\n";
    char temp[MAX_LEN] = "Hello";
    cout << "before strcat: \n";
    cout << "temp: \n";
    for (auto i : temp) {
        if (i == '\0')
            cout << "NULL" << " ";
        else
            cout << i << " ";
    }
    cout << endl;
    cout << strlen(temp) << endl << endl;

    char temp1[MAX_LEN];
    strcpy(temp1, strcat(temp, temp));

    cout << "after strcat: \n";
    cout << "temp: \n";
    for (auto i : temp) {
        if (i == '\0')
            cout << "NULL" << " ";
        else
            cout << i << " ";
    }
    cout << endl;
    cout << strlen(temp) << endl << endl;

    cout << "temp1: \n";
    for (auto i : temp1) {
        if (i == '\0')
            cout << "NULL" << " ";
        else
            cout << i << " ";
    }
    cout << endl;
    cout << strlen(temp1) << endl;


    cout << "================temp4 = strcat(Hello, World!)==========================\n";
    char temp2[MAX_LEN] = "Hello";
    cout << "before strcat: \n";
    cout << "temp2: \n";
    for (auto i : temp2) {
        if (i == '\0')
            cout << "NULL" << " ";
        else
            cout << i << " ";
    }
    cout << endl;
    cout << strlen(temp2) << endl << endl;

    char temp3[MAX_LEN] = "World!";
    cout << "temp3: \n";
    for (auto i : temp3) {
        if (i == '\0')
            cout << "NULL" << " ";
        else
            cout << i << " ";
    }
    cout << endl;
    cout << strlen(temp3) << endl << endl;

    char temp4[MAX_LEN];
    strcpy(temp4, strcat(temp2, temp3));

    cout << "after strcat: \n";
    cout << "temp2: \n";
    for (auto i : temp2) {
        if (i == '\0')
            cout << "NULL" << " ";
        else
            cout << i << " ";
    }
    cout << endl;
    cout << strlen(temp2) << endl << endl;

    cout << "temp3: \n";
    for (auto i : temp3) {
        if (i == '\0')
            cout << "NULL" << " ";
        else
            cout << i << " ";
    }
    cout << endl;
    cout << strlen(temp3) << endl << endl;

    cout << "temp4: \n";
    for (auto i : temp4) {
        if (i == '\0')
            cout << "NULL" << " ";
        else
            cout << i << " ";
    }
    cout << endl;
    cout << strlen(temp4) << endl << endl;

    return 0;
}




결과


============temp1 = strcat(Hello, Hello)==============

before strcat: 

temp: 

H e l l o NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL 

5


after strcat: 

temp: 

H e l l o H e l l o H NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL 

11


temp1: 

H e l l o H e l l o H NULL U U NULL NULL h � � � �  NULL NULL  

11

================temp4 = strcat(Hello, World!)==========================

before strcat: 

temp2: 

H e l l o NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL 

5


temp3: 

W o r l d ! NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL 

6


after strcat: 

temp2: 

H e l l o W o r l d ! NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL 

11


temp3: 

W o r l d ! NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL 

6


temp4: 

H e l l o W o r l d ! NULL U U NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL � 

11