"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
strcat 할때 널이 H 덮어 씌어져서
그러면은 strcpy(temp3, strcat(temp1, temp2)) 같은 걸 쓰고싶다면 temp3[strlen(temp1)+strlen(temp2)]='\0' 같이 뒷자리 짤라줘야하는건가? 근데 그럼 NULL 뒤에 갑자기 나오는 U들은 뭐야?
다른 영역이면 그럴필요 없지
이해가 안돼... "Hello\0"랑 "Hello\0" strcat하면 앞 Hello의 널이 짤리면서 "HelloHello\0" 돼야하는거 아냐? "HelloHelloH\0"인 이유를 모르겠다..
두개 주소가 같잖아
아 그래서 그런갑네....진짜 간단한거였네 고맙다
NULL뒤에 쓰레기 데이터들은 변수 초기화 안해서 생기는거 아니냐
근데 매번 같은 값만 나와서 이상해서..
근데 strcat(temp, temp)가 작동하긴 함?? 갑자기 궁금해서 나도 바로 테스트 해보는데 에러나던데
어거지로 돌아가서 더 헷갈린것같다 에러는 안나더라
아마 에러가 아니고 워닝일꺼임