strcpy(3) | Library Functions Manual | strcpy(3) |
stpcpy, strcpy, strcat - eine Zeichenkette kopieren oder verketten
Standard-C-Bibliothek (libc, -lc)
#include <string.h>
char *stpcpy(char *restrict Ziel, const char *restrict Quelle); char *strcpy(char *restrict Ziel, const char *restrict Quelle); char *strcat(char *restrict Ziel, const char *restrict Quelle);
stpcpy():
Seit Glibc 2.10: _POSIX_C_SOURCE >= 200809L Vor Glibc 2.10: _GNU_SOURCE
Eine Implementierung dieser Funktionen könnte wie folgt aussehen:
char * stpcpy(char *restrict Ziel, const char *restrict Quelle) { char *p; p = mempcpy(Ziel, Quelle, strlen(Quelle)); *p = '\0'; return p; } char * strcpy(char *restrict Ziel, const char *restrict Quelle) { stpcpy(Ziel, Quelle); return Ziel; } char * strcat(char *restrict Ziel, const char *restrict Quelle) { stpcpy(Ziel + strlen(Ziel), Quelle); return Ziel; }
Siehe attributes(7) für eine Erläuterung der in diesem Abschnitt verwandten Ausdrücke.
Schnittstelle | Attribut | Wert |
stpcpy(), strcpy(), strcat() | Multithread-Fähigkeit | MT-Safe |
Die Zeichenketten Quelle und Ziel dürfen sich nicht überlappen.
Falls der Zielpuffer nicht groß genug ist, dann ist das Verhalten nicht definiert. Siehe _FORTIFY_SOURCE in feature_test_macros(7).
strcat() kann sehr ineffizient sein. Lesen Sie zum Thema Shlemiel the painter.
#include <err.h> #include <stdio.h> #include <stdlib.h> #include <string.h> int main(void) { char *p; char *buf1; char *buf2; size_t len, maxsize; maxsize = strlen("Hello ") + strlen("world") + strlen("!") + 1; buf1 = malloc(sizeof(*buf1) * maxsize); if (buf1 == NULL) err(EXIT_FAILURE, "malloc()"); buf2 = malloc(sizeof(*buf2) * maxsize); if (buf2 == 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); strcpy(buf2, "Hello "); strcat(buf2, "world"); strcat(buf2, "!"); len = strlen(buf2); printf("[len = %zu]: ", len); puts(buf2); // "Hello world!" free(buf2); exit(EXIT_SUCCESS); }
strdup(3), string(3), wcscpy(3), string_copying(7)
Die deutsche Übersetzung dieser Handbuchseite wurde von Markus Schmitt <fw@math.uni-sb.de>, Dr. Tobias Quathamer <toddy@debian.org>, Helge Kreutzmann <debian@helgefjell.de>, Martin Eberhard Schauer <Martin.E.Schauer@gmx.de> und Mario Blättermann <mario.blaettermann@gmail.com> erstellt.
Diese Übersetzung ist Freie Dokumentation; lesen Sie die GNU General Public License Version 3 oder neuer bezüglich der Copyright-Bedingungen. Es wird KEINE HAFTUNG übernommen.
Wenn Sie Fehler in der Übersetzung dieser Handbuchseite finden, schicken Sie bitte eine E-Mail an die Mailingliste der Übersetzer.
5. Februar 2023 | Linux man-pages 6.03 |