STRCPY(3POSIX)
π STRCPY β ok bestie, let's copy a string π
So you opened the POSIX Programmer's Manual page for stpcpy and strcpy and honestly? iconic choice. Sit down, grab a boba, let's get into it. π§β¨
πΈ PROLOG πΈ
Real talk first: this manual page is part of the POSIX Programmer's Manual. The Linux implementation of this interface may differ (so like, go check the corresponding Linux manual page if you need the Linux tea β), or the interface may not even be implemented on Linux. Not every baddie ships everywhere, you know? π
β¨ NAME β¨
stpcpy, strcpy β copy a string and return a pointer to the end of the result π
π¦ SYNOPSIS π¦
#include <string.h>
char *stpcpy(char *restrict s1, const char *restrict s2);
char *strcpy(char *restrict s1, const char *restrict s2);
Look at those restrict qualifiers. So demure. So mindful. π«Ά
π DESCRIPTION π
Okay so for strcpy() specifically: the functionality described on this reference page is aligned with the ISO C standard. Any conflict between the requirements described here and the ISO C standard is unintentional β no drama intended, fr fr. This volume of POSIX.1β2017 defers to the ISO C standard. She knows her place and it's β¨respectfulβ¨.
The stpcpy() and strcpy() functions shall copy the string pointed to by s2 β including the terminating NUL character, don't forget the NUL, she's part of the package β into the array pointed to by s1.
β οΈ And now the scary part: if copying takes place between objects that overlap, the behavior is undefined. Not "kinda weird", not "a little off" β undefined. Absolutely no cap. Do not do this. It's giving chaos. π
π RETURN VALUE π
- The
stpcpy()function shall return a pointer to the terminatingNULcharacter copied into the s1 buffer. She hands you the end of the party, not the start. π - The
strcpy()function shall return s1. Simple. Classic. The original main character. π
And listen β no return values are reserved to indicate an error. So if you were waiting for an error code, bestie, there isn't one. That's just not the vibe here. π
π ERRORS π
No errors are defined.
That's it. That's the section. Nothing to report, everything's fine, we move. π
The following sections are informative.
π EXAMPLES π
π§ Construction of a Multi-Part Message in a Single Buffer
#include <string.h>
#include <stdio.h>
int
main (void)
{
char buffer [10];
char *name = buffer;
name = stpcpy (stpcpy (stpcpy (name, "ice"),"-"), "cream");
puts (buffer);
return 0;
}
Nested stpcpy calls like little matryoshka dolls β each one returns the end of what it just wrote, so you can chain them and build "ice-cream" in one go. The way that *works*β¦ obsessed. π¨
πΈ Initializing a String
The following example copies the string "----------" into the permstring variable.
#include <string.h>
...
static char permstring[11];
...
strcpy(permstring, "----------");
...
Eleven characters of buffer, ten dashes and one NUL. Math is math. β¨
π Storing a Key and Data
The following example allocates space for a key using malloc() then uses strcpy() to place the key there. Then it allocates space for data using malloc(), and uses strcpy() to place data there. (The user-defined function dbfree() frees memory previously allocated to an array of type struct element *.)
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
...
/* Structure used to read data and store it. */
struct element {
char *key;
char *data;
};
struct element *tbl, *curtbl;
char *key, *data;
int count;
...
void dbfree(struct element *, int);
...
if ((curtbl->key = malloc(strlen(key) + 1)) == NULL) {
perror("malloc"); dbfree(tbl, count); return NULL;
}
strcpy(curtbl->key, key);
if ((curtbl->data = malloc(strlen(data) + 1)) == NULL) {
perror("malloc"); free(curtbl->key); dbfree(tbl, count); return NULL;
}
strcpy(curtbl->data, data);
...
See how it's strlen(key) + 1? That + 1 is for the NUL terminator. Never forget the + 1. Never. It's the little detail that saves your whole program from disaster. π
π APPLICATION USAGE π
Character movement is performed differently in different implementations. So overlapping moves may yield surprises. And by "surprises" we mean "things you did not ask for". π
This version is aligned with the ISO C standard; this does not affect compatibility with XPG3 applications. Reliable error detection by this function was never guaranteed. She's not a detective, she's a copier. Manage your expectations accordingly. π
π₯ RATIONALE π₯
None.
She said what she said and she's not elaborating. Periodt. π
β FUTURE DIRECTIONS β
None.
The future? Unchanged. Stable. Unbothered. Moisturized. In her lane. Focused. Flourishing. π±
π«Ά SEE ALSO π«Ά
strncpy()wcscpy()
And from the Base Definitions volume of POSIX.1β2017: <string.h> π
π COPYRIGHT π
Portions of this text are reprinted and reproduced in electronic form from IEEE Std 1003.1-2017, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 7, 2018 Edition, Copyright (C) 2018 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. In the event of any discrepancy between this version and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. The original Standard can be obtained online at http://www.opengroup.org/unix/online.html .
Any typographical or formatting errors that appear in this page are most likely to have been introduced during the conversion of the source files to man page format. To report such errors, see https://www.kernel.org/doc/man-pages/reporting_bugs.html .
Okay bestie that's the whole page, we ate and left no crumbs π½οΈβ¨. This was IEEE/The Open Group, 2017, and the page is STRCPY(3POSIX). Stay safe, never overlap your buffers, and always add that + 1. Love u, bye!! πππ¦πΈπ
π«Ά
π¦ where this page comes from
- debian bookworm: manpages-posix-dev 2017a-2 Β· Other
- debian trixie: manpages-posix-dev 2017a-2 Β· Other
- ubuntu jammy: manpages-posix-dev 2017a-2 Β· Other
- ubuntu noble: manpages-posix-dev 2017a-2 Β· Other
- ubuntu resolute: manpages-posix-dev 2017a-2build1 Β· Other
- ubuntu stonking: manpages-posix-dev 2017a-2.1 Β· Other
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms