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