On this page
strcpy
Prev: strdup | Up: string.h | Next: strcat
Rel: snprintf(), string.h, stdlib.h & string.h Guide
Ref: cppreference — strcpy
Tags: #public #prog #c #stdlib #string #tier/legacy
Include
#include <string.h>
Signature & Overview
char *strcpy(char *restrict dst, const char *restrict src);
Unbounded string copy with no destination size check.
Legacy / Avoid: Prefer snprintf() in all new code.
Notes & Pitfalls
- Unbounded write (classic buffer overflow): Prefer
snprintf(dst, sizeof(dst), "%s", src)or checkstrlen(src) + 1 <= cap+memcpy().
Example
#include <assert.h>
#include <stdio.h>
#include <string.h>
int main(void) {
char dst[16];
snprintf(dst, sizeof(dst), "%s", "safe copy");
assert(strcmp(dst, "safe copy") == 0);
}
Linked from
Related notes
- tuts/c/stdlib/string/strcat Linked here; links to this page; shared tags: #c, #prog, #stdlib, #string, #tier
- tuts/c/stdlib/string/strdup Linked here; links to this page; shared tags: #c, #prog, #stdlib, #string, #tier
- tuts/c/stdlib/string Linked here; links to this page; shared tags: #c, #prog, #stdlib, #string
- tuts/c/stdlib/stdio/snprintf Linked here; shared tags: #c, #prog, #stdlib, #tier
- tuts/c/stdlib/stdlib_and_string_h Linked here; shared tags: #c, #prog, #stdlib
- tuts/c/stdlib/string/memchr shared tags: #c, #prog, #stdlib, #string, #tier
- tuts/c/stdlib/string/memcmp shared tags: #c, #prog, #stdlib, #string, #tier
- tuts/c/stdlib/string/memcpy shared tags: #c, #prog, #stdlib, #string, #tier
- tuts/c/stdlib/string/memmove shared tags: #c, #prog, #stdlib, #string, #tier
- tuts/c/stdlib/string/memset shared tags: #c, #prog, #stdlib, #string, #tier