std::wcsncpy(3)
π NAME
std::wcsncpy β bestie, it's literally std::wcsncpy, that's the whole name, we're starting strong β¨
This one lives in the C++ Standard Library (yes, the C++ one, not plain C, iykyk) π
β¨ SYNOPSIS β¨
Okay so first thing, this baby is defined in header <cwchar> β don't forget to include it or the compiler will simply not let you have your moment π
wchar_t* wcsncpy( wchar_t* dest, const wchar_t* src, std::size_t count );
Here's the whole vibe of what it does, fr fr:
It copies at most count characters of the wide string pointed to by src β and yes that includes the terminating null wide character β over to the wide character array pointed to by dest πΈ
Now the chaotic part, pay attention:
- If
countgets reached before the entire stringsrcwas copied, the resulting wide character array is not null-terminated. Not a drill. It's just out there, unterminated, living its life π - If, after copying the terminating null wide character from
src,countis not reached, then additional null wide characters are written todestuntil the total ofcountcharacters have been written. So it pads. It's an overachiever π«Ά - And if the strings overlap? Behavior is undefined. Periodt. Do not do that, bestie, I'm begging π
π¦ PARAMETERS π¦
destβ pointer to the wide character array you're copying to (the destination, the main character of this operation π)srcβ pointer to the wide string you're copying from (the source, the vibe provider)countβ maximum number of wide characters to copy (the budget, the limit, the cap π )
π RETURN VALUE π
It returns dest. That's it. That's the return value. Slay π
πΈ NOTES πΈ
In typical usage, count is the size of the destination array. Lowkey the most normal way to use this, ngl β
π₯ EXAMPLE π₯
// Run this code β go on, run it, I'll wait π
#include <cwchar>
#include <iostream>
int main()
{
const wchar_t src[] = L"hi";
wchar_t dest[6] = {L'a', L'b', L'c', L'd', L'e', L'f'};
std::wcsncpy(dest, src, 5); // this will copy 'hi' and repeat \0 three times
std::wcout << "The contents of dest are: ";
for (const wchar_t c : dest)
{
if (c)
std::wcout << c << ' ';
else
std::wcout << "\\0" << ' ';
}
std::wcout << '\n';
}
Notice that comment on the std::wcsncpy line β it's doing the heavy lifting of explaining: this will copy 'hi' and repeat \0 three times. Iconic behavior honestly π
β OUTPUT β
The contents of dest are: h i \0 \0 \0 f
See that f at the end? That's the one character from the original array that survived because count was 5 and it never got touched. Main character energy, honestly π§
π SEE ALSO π
wcscpyβ copies one wide string to another (function)wmemcpyβ copies a certain amount of wide characters between two non-overlapping arrays (function) β emphasis on non-overlapping, bestie, they mean it πstrncpyβ copies a certain amount of characters from one string to another (function) β the narrow-char cousin of this whole situation- C documentation for
wcsncpyβ the original lore, if you want to go deeper π
Alright bestie, that's the whole page π«Ά Go forth and copy your wide strings responsibly, remember the overlap rule is undefined behavior (no cap), and don't forget that unterminated array thing β that's the one that gets people π
Also a tiny shoutout: the terminator is only guaranteed whensrcis shorter thancount, so when in doubt, check yourcount. That's just the notes section again but with feeling π
This lil page came from http://cppreference.com, dated 2024.06.10, and lives under std::wcsncpy(3) β bye, ily, go write some wchar_t code π¦β¨
π¦ 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