The strcpy function is used to copy strings. Its prototype is: char strcpy(char destination, const char* source); Usage method: Pass the destination string address and source string as parameters. Note: Make sure there is enough space in the target string, null characters are not automatically added, and the behavior when overlapping strings is undefined. Alternative functions: strncpy and strlcpy are safer, prevent buffer overflows and truncate lengthy source strings.
strcpy function usage in c
strcpy
function is used for copying in C one string into another string. Its prototype is:
<code class="cpp">char* strcpy(char* destination, const char* source);</code>
where:
destination
: target string (the string to be copied to) source
: Source string (string to be copied)Usage method
To use the strcpy
function, just Just pass the address of the target string and the source string as parameters:
<code class="cpp">char dest[100]; char src[] = "Hello World"; strcpy(dest, src);</code>
After executing this code, the string "Hello World" will be copied to dest
.
Notes
The following things need to be noted:
strcpy
will not automatically add the null character '\0' after the target string, so you need to add it yourself before using the target string. strcpy
is undefined if the destination string and source string overlap. Replacement functions
In C, the strcpy
function has been replaced by strncpy
and strlcpy
Replaced by safer alternative functions. These functions provide better protection against buffer overflows and truncate the copy if the source string is longer than the destination string.
The above is the detailed content of Usage of strcpy function in c++. For more information, please follow other related articles on the PHP Chinese website!