在线文档教程
C++
字符串 | Strings

std::strncpy

STD:strncpy

Defined in header
char *strncpy( char *dest, const char *src, std::size_t count

最多拷贝count指向的字节字符串的字符。src%28包括将空字符%29终止到dest...

如果count在整个字符串之前到达。src复制后,生成的字符数组不会以空结尾。

如果,在从src,,,count未到达,则会将其他空字符写入dest直到...的总数count文字已经写好了。

如果字符串重叠,则行为未定义。

参数

dest-pointer to the character array to copy to
src-pointer to the byte string to copy from
count-maximum number of characters to copy

返回值

dest...

二次

#include <iostream> #include <cstring> int main() { const char* src = "hi"; char dest[6] = {'a', 'b', 'c', 'd', 'e', 'f'}; std::strncpy(dest, src, 5 std::cout << "The contents of dest are: "; for (char c : dest) { if (c) { std::cout << c << ' '; } else { std::cout << "\\0" << ' '; } } std::cout << '\n'; }

二次

产出:

二次

The contents of dest are: h i \0 \0 \0 f

二次

另见

strcpycopies one string to another (function)
memcpycopies one buffer to another (function)

c strncpy文档

© cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

http://en.cppreference.com/w/cpp/string/字节/strncpy