EN JA
STRCAT(3)
STRCAT(3) Linux Programmer's Manual STRCAT(3)

名前

strcat, strncat -二つの文字列を連結する

書式


#include <string.h>
 

char *strcat(char * dest , const char * src );
 

char *strncat(char * dest , const char * src , size_t n );

説明

The strcat() function appends the src string to the dest string, overwriting the terminating null byte ('\0') at the end of dest, and then adds a terminating null byte. The strings may not overlap, and the dest string must have enough space for the result. If dest is not large enough, program behavior is unpredictable; buffer overruns are a favorite avenue for attacking secure programs.

strncat() も同様だが、以下の点が異なる。

*
src のうち最大 n バイトが使用される。
*
srcn バイト以上の場合、 src は NULL 終端されている必要はない。

strcat() と同じく、 dest に格納される結果の文字列は常に NULL 終端される。

srcn バイト以上の場合、 strncat() は destn+1 バイトを書き込む ( src からの n バイトと終端の NULL バイトである)。したがって、 dest の大きさは最低でも strlen(dest)+n+1 でなければならない。

 

strncat() の簡単な実装は以下のような感じであろう:


char*
strncat(char *dest, const char *src, size_t n)
{
size_t dest_len = strlen(dest);
size_t i;


for (i = 0 ; i < n && src[i] != '\0' ; i++)
dest[dest_len + i] = src[i];
dest[dest_len + i] = '\0';


return dest;
}

返り値

strcat() 関数と strncat() 関数は、結果としてできる文字列 dest へのポインタを返す。

準拠

SVr4, 4.3BSD, C89, C99.

注意

Some systems (the BSDs, Solaris, and others) provide the following function:
 

size_t strlcat(char *dest, const char *src, size_t size);
 
This function appends the null-terminated string src to the string dest, copying at most size-strlen(dest)-1 from src, and adds a null terminator to the result, unless size is less than strlen(dest). This function fixes the buffer overrun problem of strcat(), but the caller must still handle the possibility of data loss if size is too small. The function returns the length of the string strlcat() tried to create; if the return value is greater than or equal to size, data loss occurred. If data loss matters, the caller must either check the arguments before the call, or test the function return value. strlcat() is not present in glibc and is not standardized by POSIX, but is available on Linux via the libbsd library.

関連項目

bcopy(3), memccpy(3), memcpy(3), strcpy(3), string(3), strncpy(3), wcscat(3), wcsncat(3)

この文書について

この man ページは Linux man-pages プロジェクトのリリース 3.51 の一部である。プロジェクトの説明とバグ報告に関する情報は http://www.kernel.org/doc/man-pages/ に書かれている。
2012-07-19 GNU