Name

strcpy

Synopsis

Copies a string to another location

#include <string.h>
char *strcpy( char * restrict dest, const char * restrict src );

The strcpy() function copies the string addressed by src to the char array addressed by dest, and returns the value of its first argument, which points to the new copy of the string.

There is no limit to the number of characters strcpy() may write before it encounters a null character in the source string. It is up to you the programmer to make sure there is enough storage available to accommodate the string, including its terminator character. Consider using strncpy() instead to reduce the risk of buffer overflows. You must also make sure that the locations that strcpy() reads from and writes to do not overlap.

Example

struct guest { char name[64]; int age; _Bool male, smoking, discount; } this;
int result;

printf( "Last name: " );
result = scanf( "%[^\n]", this.name );
if ( result < 1 )strcpy( this.name,  "[not available]" );

See Also

The strncpy(), memcpy(), memmove(), wcscpy(), wcsncpy(), wmemcpy(), and wmemmove() functions; and the examples at atof(), malloc(), and strxfrm()

Get C in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.