std::wcsncpy(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

πŸ’… 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 count gets reached before the entire string src was 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, count is not reached, then additional null wide characters are written to dest until the total of count characters 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 when src is shorter than count, so when in doubt, check your count. 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