strcpy(3) | Library Functions Manual | strcpy(3) |
stpcpy, strcpy, strcat - Copier ou concaténer une chaîne
Bibliothèque C standard (libc, -lc)
#include <string.h>
char *stpcpy(char *restrict dst, const char *restrict src); char *strcpy(char *restrict dst, const char *restrict src); char *strcat(char *restrict dst, const char *restrict src);
stpcpy() :
Depuis la glibc 2.10 : _POSIX_C_SOURCE >= 200809L Avant la glibc 2.10 : _GNU_SOURCE
Une implémentation de ces fonctions pourrait être :
char * stpcpy(char *restrict dst, const char *restrict src) { char *p; p = mempcpy(dst, src, strlen(src)); *p = '\0'; return p; } char * strcpy(char *restrict dst, const char *restrict src) { stpcpy(dst, src); return dst; } char * strcat(char *restrict dst, const char *restrict src) { stpcpy(dst + strlen(dst), src); return dst; }
Pour une explication des termes utilisés dans cette section, consulter attributes(7).
Interface | Attribut | Valeur |
stpcpy(), strcpy(), strcat() | Sécurité des threads | MT-Safe |
Les chaînes src et dst ne doivent pas se chevaucher.
Si le tampon de destination n'est pas assez grand, le comportement est indéfini. Voir _FORTIFY_SOURCE dans feature_test_macros(7).
strcat() peut être très inefficace. Lire à ce sujet 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)
La traduction française de cette page de manuel a été créée par Christophe Blaess <https://www.blaess.fr/christophe/>, Stéphan Rafin <stephan.rafin@laposte.net>, Thierry Vignaud <tvignaud@mandriva.com>, François Micaux, Alain Portal <aportal@univ-montp2.fr>, Jean-Philippe Guérard <fevrier@tigreraye.org>, Jean-Luc Coulon (f5ibh) <jean-luc.coulon@wanadoo.fr>, Julien Cristau <jcristau@debian.org>, Thomas Huriaux <thomas.huriaux@gmail.com>, Nicolas François <nicolas.francois@centraliens.net>, Florentin Duneau <fduneau@gmail.com>, Simon Paillard <simon.paillard@resel.enst-bretagne.fr>, Denis Barbier <barbier@debian.org>, David Prévot <david@tilapin.org>, Frédéric Hantrais <fhantrais@gmail.com>, Grégoire Scano <gregoire.scano@malloc.fr> et Jean-Pierre Giraud <jean-pierregiraud@neuf.fr>
Cette traduction est une documentation libre ; veuillez vous reporter à la GNU General Public License version 3 concernant les conditions de copie et de distribution. Il n'y a aucune RESPONSABILITÉ LÉGALE.
Si vous découvrez un bogue dans la traduction de cette page de manuel, veuillez envoyer un message à debian-l10n-french@lists.debian.org.
5 février 2023 | Pages du manuel de Linux 6.03 |