名稱空間
變體
操作

std::strcpy

來自 cppreference.com
< cpp‎ | string‎ | byte
在標頭檔案 <cstring> 中定義
char* strcpy( char* dest, const char* src );

src 所指向的字串(包括空終止符)複製到 dest 所指向的字元陣列的第一個元素。

如果 dest 陣列不夠大,則行為未定義。如果字串重疊,則行為未定義。

目錄

[編輯] 引數

dest - 指向要寫入的字元陣列的指標
src - 指向要複製的以空字元結尾的位元組字串的指標

[編輯] 返回值

dest

[編輯] 示例

#include <cstring>
#include <iostream>
#include <memory>
 
int main()
{
    const char* src = "Take the test.";
//  src[0] = 'M'; // can't modify string literal
    auto dst = std::make_unique<char[]>(std::strlen(src) + 1); // +1 for null terminator
    std::strcpy(dst.get(), src);
    dst[0] = 'M';
    std::cout << src << '\n' << dst.get() << '\n';
}

輸出

Take the test.
Make the test.

[編輯] 參閱

從一個字串複製一定數量的字元到另一個字串
(函式) [編輯]
將一個緩衝區複製到另一個緩衝區
(函式) [編輯]
C 文件 關於 strcpy