strdup

Section: C Library Functions (3)
Updated: 2026-02-25
Index JM Home Page roff page
 

名前

strdup, strndup - duplicate a string  

ライブラリ

標準 C ライブラリ (libc,~-lc)  

書式

#include <string.h>

char *strdup(const char *s);

char *strndup(size_t n;
              const char s[n], size_t n);

glibc 向けの機能検査マクロの要件 (feature_test_macros(7) 参照):

strdup():


    _XOPEN_SOURCE >= 500
        || /* Since glibc 2.12: */ _POSIX_C_SOURCE >= 200809L
        || /* glibc <= 2.19: */ _BSD_SOURCE || _SVID_SOURCE

strndup():

    Since glibc 2.10:
        _POSIX_C_SOURCE >= 200809L
    Before glibc 2.10:
        _GNU_SOURCE
 

説明

strdup() 関数は、文字列 sの複製である 新しい文字列へのポインターを返す。 新しい文字列のためのメモリーは malloc(3) で得ている。 そして、 free(3) で解放することができる。

The strndup() function is similar, but copies at most n bytes. If s is longer than n, only n bytes are copied, and a terminating null byte ('\0') is added.  

返り値

On success, the strdup() function returns a pointer to the duplicated string. It returns NULL if insufficient memory was available, with errno set to indicate the error.  

エラー

ENOMEM
複製された文字列を割り当てる十分なメモリーが確保できなかった。
 

属性

この節で使用されている用語の説明については、 attributes(7) を参照。
インターフェース属性値
strdup(), strndup() Thread safetyMT-Safe
 

標準規格

POSIX.1-2008.  

歴史

strdup()
SVr4, 4.3BSD-Reno, POSIX.1-2001.
strndup()
POSIX.1-2008.
 

関連項目

alloca(3), calloc(3), free(3), malloc(3), realloc(3), string(3), strdupa(3), strndupa(3), wcsdup(3)


 

Index

名前
ライブラリ
書式
説明
返り値
エラー
属性
標準規格
歴史
関連項目

This document was created by man2html, using the manual pages.