사용법
1 2 3 4 | #include <string.h> char * strcat ( char * destination, const char * source ); char * strcat ( char * destination, const char * source ); | cs |
설명
destination 끝에 source를 덧붙인다.
이때,
destination 의 맨 마지막 Null 문자는 source 의 첫 번째 문자가 덮어 씌우게 된다.
그리고, source 의 마지막 Null 문자가 destination 끝에 붙어서 새로운 문자열을 형성하게 된다.
예를 들면, destination = "hello"이고, source="world"였다면 strcat 후, destination 은 "helloworld" 가 된다.
strncat의 경우 strcat과 마찬가지로 2개의 문자열 인수를 받아 1개의 문자열로 만든다.
단, strncat의 경우 복사할 src 문자열 중에 앞에서 몇 번째까지 붙여 넣을지 지정할 수 있다.
주의해야 할 점은 합쳐진 문자열을 첫 번째 인수의 포인터 배열에 저장된다는 것이다. 그러므로 첫 번째 인수의 포인터나 배열은 합쳐진 문자열 크기만큼의 메모리를 확보해야 한다. 즉, 첫 번째와 두 번째 인수의 문자열 크기를 확인하고 새로운 메모리를 확보하여 합쳐진 문자열을 넣지 않는다는 뜻이다.
strncat는 첫 번째 인수의 메모리에서 '\0'을 찾아 그 부분부터 2번째 인수의 문자열의 문자를 지정한 개수만큼 차례로 복사해 나간다.
예제(strcat)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | #include <stdio.h> #include <string.h> int main (void) { char str[25]; strcpy(str,"Hello "); strcat(str,"World"); printf(str); return 0; } | cs |
예제(strncat)
1 2 3 4 5 6 7 8 9 10 11 | #include <stdio.h> #include <string.h> int main( void) { char str[20] = "hello"; printf( "%s\n", strncat( str1, " world!", 7)); return 0; } | cs |
'programming Language > [C] 함수정리' 카테고리의 다른 글
[strcpy&strncpy] (0) | 2016.01.09 |
---|
WRITTEN BY