liveBook · Manning

Strcpy

As explained in Modern C, Third Edition

strcpy is a C standard-library function that copies a null-terminated string into destination storage.

strcpy(target, source) copies the null-terminated string in source into target, including its terminating 0 character. Unlike memcpy, it needs no length argument: it stops at the source string's terminator. section 6.1.5

Copying a string

char target[4]; strcpy(target, "jay"); The destination has room for three letters and the terminating 0. A smaller target would not be large enough for the copy. section 6.1.5

size_t strlen(char const s[static 1]);
char*  strcpy(char target[static 1], char const source[static 1]);
char*  strdup(char const s[static 1]);
char*  strndup(char const s[static 1], size_t n);
signed strcmp(char const s0[static 1], char const s1[static 1]);
signed strcoll(char const s0[static 1], char const s1[static 1]);
char* strchr(const char s[static 1], int c);
size_t strspn(const char s1[static 1], const char s2[static 1]);
size_t strcspn(const char s1[static 1], const char s2[static 1]);

The declaration shows strcpy returning the destination pointer and requiring valid, nonempty character-array arguments. section 6.1.5

How Modern C, Third Edition covers it

How deep it goes
Defines the stopping rule and destination-size requirement, but does not develop allocation or implementation internals.
Examples
Uses character arrays and the string jay to illustrate termination and destination capacity.
What it assumes
Assumes familiarity with character arrays and requires understanding that strings end at a 0 character.
What you can do after
Apply strcpy through the C standard library when the source is terminated and the destination is sufficiently large.
How it connects
Relates strcpy to memcpy, strlen, strdup, strndup, and null-terminated strings.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Modern C, Third Edition. The section links point to the parts of the book it draws on.