#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);
Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
stpcpy():
Since glibc 2.10: _POSIX_C_SOURCE >= 200809L Before glibc 2.10: _GNU_SOURCE
An implementation of these functions might be:
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;
}
Interface | Attribute | Value |
stpcpy(), strcpy(), strcat() | Thread safety | MT-Safe |
If the destination buffer is not large enough, the behavior is undefined. See _FORTIFY_SOURCE in feature_test_macros(7).
strcat() can be very inefficient. Read about Shlemiel the painter
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);