What is the strncpy() Function in C++
The strncpy() function is a built-in C++ function that is used to copy a fixed amount of characters from one string to another. The function needs three parameters: the destination string that will hold the characters, the source string that will provide the characters, and the count of characters to copy. If the source string is less than the specified number of characters, the destination string will be padded with null characters to the remaining length.
The prototype of a strncpy() function is given below.
What are the Parameters of C++ strncpy()
All parameters of strncpy() function are explained below.
- dest: A pointer to the destination array to which the content has been
- src: A pointer to the source array from which the content is
- count: The highest number of characters that might be copied from the source to destination.
How Does strncpy() Function Work in C++
strncpy() function accepts three arguments: dest, src, and count. It checks if the src string is null-terminated, and if yes, it copies the specified number of characters from the src string to the dest string. If the count is less than the length of the src string, the first count characters have been transferred to dest string and are not null-terminated. If the count exceeds the length of src, all characters from src are copied to dest, and extra terminating null characters have been included until all of the count characters are written.
The given example illustrates the working of the C++ strncpy() function.
#include<string.h>
using namespace std;
int main()
{
char src_str[]="I am writting a code for strncpy ";
char dest_str[60];
strncpy(dest_str,src_str,strlen(src_str));
cout<<dest_str<<"\n";
return 0;
}
In this program, we used <string.h> header file because it includes strncpy() function. We defined two strings one is src_str and the other is dest_str. strncpy() function that takes data from src_str string and copies it to the dest_str. Here strlen(src_str) is used to count the total number of characters from the src_str string.
The output of the program can be shown below.
What are the Problems with Using strncpy() in C++
- If there is no null character in the destination array or the string is not null-terminated, our program or code may encounter problems sooner or later. A non-null terminated string in C++ has become risky code that may break at any moment during program execution. It may result in a segmentation fault in the program. As a result, strncpy() does not ensure that the destination string is always null-terminated, making it risky code for the program we’re writing.
- This function fails to check for overflow, so if we attempt to copy the source string to a destination that is smaller in size than the source, we get an error and undefined behavior.
Conclusion
To copy data from one string to another, we use strncpy() in C++ which is a built-in function in <string.h> header file. In this article, we have discussed syntax and working of the C++ strncpy function with an example. We also highlighted some issues due to which the strncpy function can make our code risky.