The strcpy function is a function used for string copying in C language. Its function is to copy the contents of the source string to the target string. Its usage is strcpy(char dest, const char src), where dest is the destination string address and src is the source string address. Things to note include ensuring that the destination string has enough space to accommodate the source string, and that the source string is null-terminated.
Function of strcpy function
The strcpy function is a standard library function in C language for string copying. Its function is to copy the contents of one string (source string) to another string (target string).
Usage
The prototype of the strcpy function is as follows:
<code class="c">char *strcpy(char *dest, const char *src);</code>
Among them:
dest
: The address of the target string. src
: The address of the source string. Return value
The strcpy function returns a pointer to the target string dest
.
Working principle
The strcpy function copies the contents of the source string src
character by character to the target string dest
Come to work. When copying is complete or a null character ('\0') is encountered, copying stops and adds a null character to the end of the target string.
Things to note
dest
There must be enough space to accommodate the source stringsrc# The contents of ##, including null characters.
does not have enough space to accommodate the contents of the source string
src, a buffer overflow error may occur. The
is null-terminated. Therefore, it is important to ensure that the source string is null-terminated.
.
Example
The following example shows how to use the strcpy function to copy a string:<code class="c">#include <stdio.h> #include <string.h> int main() { char dest[100]; char src[] = "Hello World"; strcpy(dest, src); printf("Copied string: %s\n", dest); return 0; }</code>
<code>Copied string: Hello World</code>
The above is the detailed content of Function of strcpy in c language. For more information, please follow other related articles on the PHP Chinese website!