C++ strcpy_s함수를 이용하여 두 문장을 어떤 문자열 포인터에 복사할때 질문
조회수 1824회
Add(char pszLeft, char pszRight) 라는 함수 내부에서 char*pszResult에 두 문자열을 합칠때,
int nLenLeft = strlen(pszLeft);
int nLenRight = strlen(pszRight);
char *pszResult = new char[nLenLeft+nLenRight+1]; 이렇게 메모리를 할당해 준 후,
새로할당한 메모리에 문자열들을 복사하기 위해서
strcpy_s(pszResult, nLenLeft + 1, pszLeft);
strcpy_s(pszResult+nLenLeft, nLenRight + 1, pszRight);
이런식으로 함수를 작성하게 되는데 첫번째 strcpy_s함수는 무슨 내용인지 이해가 가나, 두번쨰 strcpy_s함수에서 "pszResult+nLenLeft" 가 왜 들어가게 되는지 이해가 가지 않습니다 ㅠㅠ 제가 혼자 생각하기로는 pszResult의 주소값에 포인터연산을 nLenLeft만큼하여 거기의 메모리 주소부터 카피를 한다고 이해했는데 맞는 생각인가요? 아니면 다른 이유인가요??
-
(•́ ✖ •̀)
알 수 없는 사용자
1 답변
-
질문하신 내용이 맞습니다.
msdn(https://msdn.microsoft.com/ko-kr/library/td1esda9.aspx)을 보시면 첫 번째 인자가 가르키는 주소(strDestination)에 복사한다고 합니다.
errno_t strcpy_s( char *strDestination, size_t numberOfElements, const char *strSource );
"The strcpy_s function copies the contents in the address of strSource, including the terminating null character, to the location that's specified by strDestination."
-
(•́ ✖ •̀)
알 수 없는 사용자
-
댓글 입력