首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

std::strncpy

Defined in header <cstring>

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...

二次

代码语言:javascript
复制
#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';
}

二次

产出:

二次

代码语言:javascript
复制
The contents of dest are: h i \0 \0 \0 f

二次

另见

strcpy

copies one string to another (function)

memcpy

copies one buffer to another (function)

c strncpy文档

代码语言:txt
复制
 © cppreference.com

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

扫码关注腾讯云开发者

领取腾讯云代金券