STRACAT(3pub)

πŸ’– STRACAT(3pub) β€” the girlies' guide to gluing strings together πŸ’–

okay bestie, sit down, hydrate, and let me tell you about stracat β€” it lives in the C Programmer's Manual, section 3pub, and it is literally the friend who brings everyone together at the party. ✨


🌸 NAME 🌸

stracat β€” concatenate many strings into an allocated memory block πŸŽ€

no cap, that's the whole vibe: lots of little strings go in, one big glowy allocated block comes out. πŸ’…


✨ SYNOPSIS ✨

#include <publib.h>
char *stracat(const char *s, ...);

that's it. that's the signature. she's mysterious, she's variadic, she contains multitudes. πŸ¦‹


πŸ’œ DESCRIPTION πŸ’œ

okay so here's the tea. stracat will allocate a block of memory with malloc and concatenate all the argument strings into this block. 🌈

the user is supposed to free the returned block. yes queen, that's on you. she allocates, you clean up. it's giving ✨shared responsibility✨. πŸ™ˆ

the end of the argument list is signalled by a null pointer. that's the little "we're done here, thanks for coming to my TED talk" marker at the end of the args. 🎀


πŸ‘‘ RETURN VALUE πŸ‘‘

  • stracat returns a pointer to the allocated block β€” slay πŸ’–
  • or NULL if it could not be allocated β€” tragic, devastating, not the moment 😭

that's it. two outcomes. main character or nothing. ⭐


🧚 EXAMPLE 🧚

so say you wanna concatenate "hello, " and "world". you might say the following, and honestly? iconic.

p = stracat("hello, ", "world", (char *)NULL);
if (p != NULL)
     puts(p);

look at that. "hello, world" just happened. we love to see it. 🫢

please note, bestie β€” it is necessary to cast NULL as in the example above, since there is no information about the type of the argument available in the prototype, and since an unadorned NULL might not be of the correct type. 😭 this is not me being dramatic, this is just how it is. (see any good C reference book or the comp.lang.c FAQ for more information.) iykyk πŸ“š

so like. (char *)NULL. with the cast. every time. the cast is the outfit that makes the whole look work. πŸ’…


🌟 SEE ALSO 🌟

go read those too, they're part of the same friend group πŸŽ€


πŸ“ AUTHOR πŸ“

Lars Wirzenius (lars.wirzenius@helsinki.fi) β€” the legend, the moment, the author πŸ’–


and that's the whole page, angel ✨ it's from Publib, in the C Programmer's Manual β€” so next time you're glueing strings together, remember: malloc'd by her, free'd by you, and cast that NULL. mwah. πŸ’‹πŸŽ€πŸ¦‹

πŸ“¦ where this page comes from