stpcpy(3)
π stpcpy β string return-offset-pointer copy π
okay bestie so this is stpcpy(3), a libc baddie from the Standard C library (libc, -lc). yes the library name is lowkey just "libc, -lc". she's that girl. β¨
β¨ SYNOPSIS β¨
#include <string.h>
char *stpcpy(char *restrict dst, const char *restrict src);
so you include <string.h>, and you get stpcpy() which takes a dst (restrict) and an src (restrict), and hands you back a char *. slay.
π Feature Test Macro Requirements for glibc π
(if you wanna know the whole vibe behind these, go peek at feature_test_macros(7))
stpcpy():
- Since glibc 2.10:
_POSIX_C_SOURCE >= 200809L
- Before glibc 2.10:
_GNU_SOURCE
like. glibc really said "you need a macro pass to get in here." π
πΈ DESCRIPTION πΈ
stpcpy() is basically strcpy(3)'s cooler cousin β except instead of returning dst like a normal person, she returns a pointer to the terminating null byte in dst. main character behavior fr fr.
she's equivalent to all of these expressions, no cap:
memset(mempcpy(dst, src, strlen(src)), '\0', 1);
strcpy(mempcpy(dst, src, strlen(src)), "");
strcpy(dst, src) + strlen(src)
like, literally the same thing three ways. pick your fighter. π₯
π«Ά RETURN VALUE π«Ά
she returns a pointer to the terminating null byte of the copied string. so not the start of dst, bestie β the end. the null byte. the little '\0' that finishes the sentence. iconic.
π ATTRIBUTES π
if you're like "wait what do these terms even mean," that's what attributes(7) is for. go read it, i'll wait. π
stpcpy()- Thread safety: MT-Safe
so she's thread safe, we love a function that doesn't start drama. π§
β STANDARDS β
POSIX.1-2008.
π HISTORY π
POSIX.1-2008.
(same energy both times, she's consistent, periodt.)
π EXAMPLES π
here's the whole thing, copy it exactly, don't be cute with it:
#include <err.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int
main(void)
{
char *p;
char *buf1;
size_t len, size;
size = strlen("Hello ") + strlen("world") + strlen("!") + 1;
buf1 = malloc(sizeof(*buf1) * size);
if (buf1 == NULL)
err(EXIT_FAILURE, "malloc()");
p = buf1;
p = stpcpy(p, "Hello ");
p = stpcpy(p, "world");
p = stpcpy(p, "!");
len = p - buf1;
printf("[len = %zu]: ", len);
puts(buf1); // "Hello world!"
free(buf1);
exit(EXIT_SUCCESS);
}
see how p keeps marching forward every call? that's the whole point of the returned pointer β you just keep appending and never look back. len = p - buf1; is literally the length. obsessed. ππ
π¦ SEE ALSO π¦
and that's the tea, bestie π this page came to you from Linux man-pages 6.20, dated 2026-08-10. stay safe out there, keep your restrict qualifiers on, and remember: the null byte is not a suggestion, it's a lifestyle. byeee πβ¨
π¦ where this page comes from
- arch rolling: man-pages 6.20-1 Β· BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
- nixpkgs unstable: man-pages 6.20
- π the page's license: Linux-man-pages-copyleft
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms