The sole purpose of the strcpy() function is to copy a string from source to destination. Now, lets us look at the above syntax of strcpy() function. The strcpy() function is capable of accepting two parameters –
- char * destination
- const char * source
The source is a constant here to ensure that the strcpy() function cannot change the source string. The strcpy() function copies all the characters (including the NULL character at the end of the string) from the source string to the destination. Once the copy operation is complete from source to destination, the strcpy() function returns the address of the destination back to the caller function.
The important point to notice here is, the strcpy() function does not append the source string with the destination string. It rather replaces the content of the destination with the content of the source string.
Also, the strcpy() function does not perform any checks to ensure that the size of the destination is more than the source string, it is completely the responsibility of the programmer.
Now, we will see several examples to understand the strcpy() function:
- strcpy() – Normal Operation (example1.c)
- strcpy() – Case-1 (example2.c)
- strcpy() – Case-2 (example3.c)
- strcpy() – Case-3 (example4.c)
- strcpy() – User Defined Version (example5.c)
- strcpy() – User Defined Version Optimized (example6.c)
strcpy() – Normal Operation (example1.c):
This example program shows how to perform a normal string copy operation using the strcpy() function in the C programming language. Please note that the length of the destination string is 30 (char destination_str; ), which is greater than the length of the source string (length is 18 including the NULL character) so that the destination can accommodate all the characters from the source string.
strcpy() – Case-1 (example2.c):
The intent of this example program is to clearly explain what happens when the length of the destination string is less than the length of the source string. In such cases, the destination location will not have enough spaces/bytes to accommodate all the characters (including NULL character) from the source string. Two things, you should always keep in mind:
- The strcpy() function will not check if the destination has enough space.
- This could be dangerous in embedded software because the strcpy() will replace the memory area beyond the destination’s boundary.
Let us look at the example program. We have declared source_str and initialized it to “www.linuxhint.com”, which will take 18 bytes in memory to store, including the Null character at the end of the string. Then, we have declared another character array i.e. destination_str with the size of only 5. So, the destination_str cannot hold the source string with a total size of 18 bytes.
But, still, we are calling the strcpy() function to copy the source string to destination string. From the below output, we can see the strcpy() did not complain at all. In this case, the strcpy() function will start copying the character from the source string (until it finds the NULL character in the source string) to the destination address (even though destination boundary exceeds). That means the strcpy() function does not do any boundary checking for destination array. Eventually, the strcpy() function will overwrite the memory addresses which are not allocated to the destination array. This is why the strcpy() function will end up overwriting the memory locations which might be allocated to a different variable.
In this example, we can see from the below output, that the strcpy() function overwrites the source string itself. Programmers should always be careful with such behavior.
strcpy() – Case-2 (example3.c):
This program illustrates the situation when the destination string size is greater than the source string size and destination string is already initialized with some value. In this example, we have initialized:
- source_str to “www.linuxhint.com” [size = 17+1 = 18]
- destination_str to “I_AM_A_DESTINATION_STRING” [size = 25+1 = 26]
The strcpy() function will copy all the 17 characters and the NULL character from the source string to the destination string. But, it will not replace/change the remaining bytes (Byte 19 to 26, one based) in the destination array. We have used for loop to iterate over the destination array and print the whole array to prove that the bytes-19 to 26 are unchanged in the destination array. That is why we see the last output as:
strcpy() – Case-3 (example4.c):
We have considered this program as an example to show that we should never call strcpy() with a string literal as the destination. This will cause undefined behavior and eventually, the program will crash.
strcpy() – User Defined Version (example5.c):
In this example program, we have shown how to write a user-defined version of strcpy() function.