STRMAXCPY(3pub)

πŸ’– STRMAXCPY(3pub) πŸ’–

okay bestie, welcome to the C Programmer's Manual ✨ this one lives in section 3pub and it's a lil string-copying icon, no cap.


🌸 NAME 🌸

strmaxcpy β€” copy at most a given number of characters of string 🫢


✨ SYNOPSIS ✨

#include <publib.h>
char *strmaxcpy(char *tgt, const char *src, size_t n);

πŸ’… DESCRIPTION πŸ’…

so basically, strmaxcpy copies up to n-1 characters from the beginning of src over into tgt, and then it tacks on a '\0' like the responsible queen it is. πŸ‘‘

n has got to be at least 1. that's the floor, bestie, no going below.

and your target string? it must be large enough to hold the result. like, actually big enough. it's giving "bring your own buffer" energy, and if you don't, that's on you. 😭

now here's the part that makes her different from strncpy(3) β€” and this is the moment, fr fr:

  • she always terminates the result with '\0'. always. every single time. periodt.
  • she does not fill the result with a bunch of extra '\0' characters. none of that padding nonsense. she's efficient. she's clean. she ate. πŸ’…

🌈 RETURN VALUE 🌈

strmaxcpy returns its first argument. that's it, that's the whole vibe. πŸŽ€


πŸ¦‹ EXAMPLE πŸ¦‹

say you wanna print out the first 69 characters of a string. here's how you might do it (tho ngl, being comfy with printf's format string might be more useful in this case πŸ™ˆ):

#include <stdio.h>
#include <publib.h>

void print42(const char *string) {
     char copy[43]; /* 42 + '\0' */

     puts(strmaxcpy(copy, string, sizeof(copy)));
}

πŸ‘‘ SEE ALSO πŸ‘‘


πŸ’œ AUTHOR πŸ’œ

Lars Wirzenius (lars.wirzenius@helsinki.fi) β€” shoutout to the legend himself 🌟


ok that's the whole page bestie!! this was brought to you by Publib and the C Programmer's Manual, filed under STRMAXCPY(3pub) β€” go forth and copy responsibly, mwah πŸ’‹πŸŽ€βœ¨
πŸ“¦ where this page comes from