std::strncpy(3)
okay so bestie, this is the std::strncpy(3) page from the C++ Standard Libary manual π
β¨ buckle up, because copying strings has lore.
πΈ NAME πΈ
std::strncpy β std::strncpy (yes, itβs giving βthe name is the name,β iconic π)
β¨ Synopsis β¨
Defined in header <cstring>
char* strncpy( char* dest, const char* src, std::size_t count );
okay so what it does: copies at most count characters of the byte string pointed to by src β and yes, that includes the terminating null character β to the character array pointed to by dest π
- If
countis reached before the entire stringsrcwas copied, the resulting character array is not null-terminated. Periodt, thatβs the rule π - If, after copying the terminating null character from
src,countis not reached, additional null characters are written todestuntil the total ofcountcharacters have been written β¨ - If the strings overlap, the behavior is undefined. Like, do not. No cap. π
π«Ά Parameters π«Ά
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 π
π Return value π
dest β thatβs it, thatβs the return value, bestie π
π Example π
// Run this code
#include <cstring>
#include <iostream>
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';
}
π Output π
The contents of dest are: h i \0 \0 \0 f
π See also π
strcpyβ copies one string to another (function) πmemcpyβ copies one buffer to another (function) β- C documentation for
strncpyβ the C docs forstrncpy, right there in the see-also list π
π source/version: http://cppreference.com Β· date: 2024.06.10 Β· page: std::strncpy(3) β thatβs the footer, bestie, and we ate π
β¨
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms