std::strncat(3)

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

std::strncat

okay so this is the std::strncat(3) page from the C++ Standard Libary, and bestie, we are gonna get you comfy with it πŸ’–βœ¨


πŸ’– NAME πŸ’–

std::strncat - std::strncat (yes, that is literally the whole name, and honestly? iconic πŸ’…)


🌈 Synopsis 🌈

Defined in header <cstring> πŸŽ€

char* strncat( char* dest, const char* src, std::size_t count );

Appends the byte string pointed to by src to the byte string pointed to by dest. At most count characters are copied. The resulting byte string is null-terminated. πŸ¦‹

The destination byte string must have enough space for the contents of both dest and src plus the terminating null character, except that the size of src is limited to count. This is the part where you double-check your buffer, bestie β€” no cap.

🚨 Undefined behavior alert: the behavior is undefined if the strings overlap. Like, not "oopsie" undefined β€” actually undefined. πŸ’€

🫢 Parameters 🫢

  • dest β€” pointer to the null-terminated byte string to append to
  • src β€” pointer to the null-terminated byte string to copy from
  • count β€” maximum number of characters to copy

πŸ‘‘ Return value πŸ‘‘

dest ✨

That's it. That's the return value. She gives you back the destination pointer, main character energy only. πŸ’…


🧚 Notes 🧚

Because std::strncat needs to seek to the end of dest on each call, it is inefficient to concatenate many strings into one using std::strncat. Like, it works, but it's giving "walking to the end of the line every single time" energy. 😭


🌸 Example 🌸

// Run this code

#include <cstdio>
#include <cstring>

int main()
{
    char str[50] = "Hello ";
    const char str2[50] = "World!";
    std::strcat(str, str2);
    std::strncat(str, " Goodbye World!", 3); // may issue "truncated output" warning
    std::puts(str);
}

⭐ Output ⭐

Hello World! Go

πŸ¦‹ See also πŸ¦‹

  • strcat β€” concatenates two strings (function)
  • strcpy β€” copies one string to another (function)
  • C documentation for strncat

mwah πŸ’‹ your girl is signing off from the std::strncat(3) page β€” source: http://cppreference.com, version/date: 2024.06.10 πŸŽ€πŸ’œ

πŸ“¦ where this page comes from