strcpy
Function Usage
Copies the source character string (including the null character (\0) at the end) to the destination. The size of the destination buffer is not checked during the copy. Therefore, if the destination buffer size is insufficient, a buffer overflow occurs.
Function Syntax
char *strcpy(char *dest, const char *src);
Parameters
Parameter |
Description |
Value Range |
Input/Output |
|---|---|---|---|
dest |
Pointer to the destination memory address for receiving the source character string. |
Non-null pointer. The pointed memory space must be writable and large enough to contain all source characters and \0. |
Input/Output |
src |
Pointer to the source memory from which data is to be copied. |
Non-null pointer, pointing to the address that stores the source character string. The value must end with \0. Otherwise, the behavior is undefined, which may cause a crash or overflow. |
Input |
Return Value
- Success: The start address of the destination character string is returned.
- Failure: See those of open source glibc. No other exception values will be returned.
Ensure that the destination buffer has sufficient space to contain the source character string (including \0 at the end). Otherwise, a memory overflow occurs.
Example
#include <stdio.h>
#include <string.h>
int main() {
char src[] = "Hello, World!";
char dest[50];
strcpy(dest, src);
printf("Source: %s\n", src);
printf("Destination: %s\n", dest);
return 0;
}
Output:
Source: Hello, World! Destination: Hello, World!