| 総合手引 | セクション 3 | English | Deutsch | オプション |
#include <string.h>
strncpy() 関数は、 len 個以上の文字は dst にコピーせず、 src の長さが len 文字以下の場合には ‘\0’ 文字を追加します。また、 src の長さが len 文字以上である場合は、 dst を終了させません。
char chararray[6];(void)strncpy(chararray, "abc", sizeof(chararray));
次の行は、 chararray を "abcdef" に設定します :
char chararray[6];(void)strncpy(chararray, "abcdefgh", sizeof(chararray));
パラメータの長さが、ソースの長さと同じか それ以上であるため chararray は NUL で終了しない ことに注意してください。
次の行は、 input から buf へ文字を適切にコピーし、 その結果を NUL で終了させます。 strncpy() の文字列自体が NUL で終了することを 保証しないためで、 これは明確にしなければなりません。
char buf[1024];(void)strncpy(buf, input, sizeof(buf) - 1); buf[sizeof(buf) - 1] = '\0';
次の例のように strlcpy(3) [英語] を使用すると、より良く実行できるかもしれません。
(void)strlcpy(buf, input, sizeof(buf));
strlcpy(3) [英語] は標準的には定義されていないので、 移植に無関係なときだけ 利用するように注意してください。
| STRCPY (3) | August 9, 2001 |
| 総合手引 | セクション 3 | English | Deutsch | オプション |
このマニュアルページサービスについてのご意見は Ben Bullock にお知らせください。 Privacy policy.
| “ | Ken Thompson has an automobile which he helped design. Unlike most automobiles, it has neither speedometer, nor gas gauge, nor any of the other numerous idiot lights which plague the modern driver. Rather, if the driver makes a mistake, a giant “?” lights up in the center of the dashboard. “The experienced driver,” says Thompson, “will usually know what's wrong.” | ” |