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);
Exigences de macros de test de fonctionnalités pour la glibc (consulter feature_test_macros(7)) :

stpcpy() :

    Depuis la glibc 2.10 :
        _POSIX_C_SOURCE >= 200809L
    Avant la glibc 2.10 :
        _GNU_SOURCE

Ces fonctions copient la chaîne pointée par src dans une chaîne dans le tampon pointé par dst. Le programmeur est responsable de l'allocation d'un tampon de destination suffisamment grand, c'est-à-dire strlen(src) + 1. Pour la différence entre les deux fonctions voir VALEUR RENVOYÉE.
Cette fonction concatène la chaîne pointée par src après la chaîne pointée par dst (écrasant son octet NULL final). Le programmeur est responsable de l'allocation d'un tampon de destination suffisamment grand, c'est-à-dire strlen(dst) + strlen(src) + 1.

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;
}

Cette fonction renvoie un pointeur vers l'octet NULL final de la chaîne copiée.
Ces fonctions renvoient 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

POSIX.1-2008.
C11, POSIX.1-2008.

POSIX.1-2008.
POSIX.1-2001, C89, SVr4, 4.3BSD.

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() can be very inefficient. Read about 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.

31 octobre 2023 Pages du manuel de Linux 6.06