strcpy() Function
The strcpy()
function in C copies a string from a source to a destination. It is part of the C standard library and is declared in the string.h
header file. The function copies characters, including the null terminator ('\0'
), from the source string to the destination array.
Warning: Using strcpy()
can lead to buffer overflows if the destination array is not large enough to store the copied string. It is the programmer’s responsibility to ensure sufficient space in the destination buffer.
Syntax of strcpy()
char *strcpy(char *destination, const char *source);
Parameters
Parameter | Description |
---|---|
destination | Pointer to the destination array where the content is to be copied. |
source | C string to be copied. |
Return Value
The strcpy()
function returns a pointer to the destination string.
Exceptions
The strcpy()
function does not perform bounds checking and may cause buffer overflow if the destination buffer is smaller than the source string.
Examples for strcpy()
Example 1: Copying a String
This example demonstrates how to use strcpy()
to copy a string from a source to a destination:
Program
#include <stdio.h>
#include <string.h>
int main() {
char source[] = "Hello, World!";
char destination[20]; // Ensure enough space for the copied string
// Copy the string from source to destination
strcpy(destination, source);
// Print the copied string
printf("Copied string: %s\n", destination);
return 0;
}
Explanation:
- A character array
source
is initialized with"Hello, World!"
. - A character array
destination
is defined with enough space to store the copied string. - The
strcpy()
function copies the contents ofsource
intodestination
. - The copied string is printed using
printf()
.
Output:
Copied string: Hello, World!
Example 2: Buffer Overflow Risk
This example demonstrates what happens when the destination buffer is too small:
Program
#include <stdio.h>
#include <string.h>
int main() {
char source[] = "This is a long string";
char destination[10]; // Insufficient space for the copied string
// Copying source into destination (unsafe)
strcpy(destination, source); // May cause buffer overflow
printf("Copied string: %s\n", destination);
return 0;
}
Explanation:
- A character array
source
contains"This is a long string"
, which has more than 10 characters. - The
destination
array is defined with only 10 characters, which is insufficient. - The
strcpy()
function copies the string, but since there is not enough space, this may cause a buffer overflow, leading to unpredictable behavior.
Output (Unpredictable Behavior):
Copied string: (corrupt output or crash)
Example 3: Using strncpy() to Prevent Buffer Overflow
To prevent buffer overflow, it is recommended to use strncpy()
instead of strcpy()
when the size of the destination buffer is limited.
Program
#include <stdio.h>
#include <string.h>
int main() {
char source[] = "This is a long string";
char destination[10]; // Limited space
// Copy safely using strncpy
strncpy(destination, source, sizeof(destination) - 1);
// Ensure null termination
destination[sizeof(destination) - 1] = '\0';
printf("Copied string: %s\n", destination);
return 0;
}
Explanation:
- The
strncpy()
function is used to copy only a limited number of characters to prevent overflow. - The last character in
destination
is explicitly set to'\0'
to ensure proper termination. - This approach prevents memory corruption and ensures safe string handling.
Output:
Copied string: This is a