Declaration of Memcpy() Function in C++
Now, let’s understand the declaration of the memcpy() function in C++ programming language. First, the function returns a void pointer to the destination buffer. Then, we call the memcpy() function. In the function brackets, we use the destination as a pointer to the memory location where we want to copy the data. The source is a pointer to the memory location where the data is currently stored. And the num is the number of bytes that we want to copy.
The void* data type that is used for both source and destination allows for flexibility in data types. The size_t data type for num ensures that the number of bytes that are copied is correctly represented.
Utilization of Memcpy() Function in C++
This program demonstrates how to use the memcpy() function in C++ to copy a string from one character array to another. The main() function is first declared, followed by the inclusion of the essential header files, iostream, and cstring.
#include <cstring>
using namespace std;
int main() {
char source[] = "Hello, World!";
char destination[20];
memcpy(destination, source, sizeof(source));
destination[sizeof(source)] = '\0';
cout << "The Source String is: " << source << endl;
cout << "The Destination String is: " << destination << endl;
return 0;
}
In the main() function, two character arrays are declared: source and destination. The “Hello, World!” text is used as the source’s initialization value, while the destination is now empty. The contents of the source are then copied into the destination using the memcpy() method. The arguments that are passed to memcpy() are the destination buffer destination, the source buffer source, and the size of the source buffer sizeof(source).
After the memcpy() operation is complete, we add a null terminator at the end of the destination so that the string is properly terminated. Finally, we print out the source and destination strings using the cout statements to verify that the copy is successful. When the program is executed, it outputs the following:
Copying the Contents of a Struct to Another Struct Using the Memcpy() Function
This C++ code demonstrates the use of the memcpy() function to copy the contents of a struct which is “Employee” to another struct which is the “upt_employee_data”. We include the basic header files in the program. Then, the “Employee” struct is defined with three members: an int id, a character array name of size 50, and a float salary.
#include <cstring>
using namespace std;
struct Employee {
int id;
char name[50];
float salary;
};
int main() {
Employee old_employee_data= {1, "John Doe", 5000.0};
Employee upt_employee_data;
memcpy(&upt_employee_data, &old_employee_data, sizeof(Employee));
upt_employee_data.salary = 6000.0;
cout << "Old Data of Employee: " << old_employee_data.id << " " << old_employee_data.name << " " << old_employee_data.salary << endl;
cout << "Updated Data of Employee: " << upt_employee_data.id << " " << upt_employee_data.name << " " << upt_employee_data.salary << endl;
return 0;
}
In the main() function, an “Employee” variable which is “old_employee_data” is initialized with the id value of 1, the name value of “John Doe”, and the salary value of 5000.0. Another “Employee” variable which is the “upt_employee_data” is declared but not initialized. The memcpy() function is then used to copy the contents of “old_employee_data” to “upt_employee_data”.
The first argument to memcpy() is the destination address (&upt_employee_data). The second argument is the source address (&old_employee_data). And the third argument is the size of the struct (sizeof(Employee)). After the copy, the salaried member of “upt_employee_data” is changed to 6000.0. Finally, the original values of “old_employee_data” and the updated values of “upt_employee_data” are printed out to the console using cout as you can see in the following:
Copying the Content of One Array to Another Array
This is an example that demonstrates how to use the memcpy() function to copy the contents of one array to another. The beginning of a C++ program that includes the standard input/output is the iostream library and the C-style string library cstring. Then, we use the line using the std namespace which tells the compiler that we use the standard namespace std which includes various standard functions and objects such as cout to print the output to the console.
#include <cstring>
using namespace std;
int main() {
int source_array[] = {1, 2, 3, 4, 5, 6, 7};
int destination_array[] = {8, 9, 10, 11, 12, 13, 14};
cout << "The Source Array is: ";
for (int i = 0; i < 7; ++i) {
std::cout << source_array[i] << " ";
}
cout << endl;
cout << "The Old Destination Array is: ";
for (int i = 0; i < 7; ++i) {
cout << destination_array[i] << " ";
}
cout << endl << endl;
memcpy(destination_array, source_array, sizeof(source_array));
cout << "The Updated Destination Array is: ";
for (int i = 0; i < 7; ++i) {
cout << destination_array[i] << " ";
}
cout << endl;
return 0;
}
Since the main() function is where the program starts, we then call it. We have two integer arrays in the main() body: the source_array and the destination_array. The source_array contains the values {1, 2, 3, 4, 5, 6, 7} and the destination_array contains the values {8, 9, 10, 11, 12, 13, 14}.
First, we print out the values of both arrays using the for-loop and cout. Then, we use the memcpy() function to copy the contents of the source_array to the destination_array. The first argument to memcpy() is the destination address (destination_array), the second argument is the source address (source_array), and the third argument is the size of the source_array (sizeof(source_array)).
After copying, we print out the contents of the destination_array again to show that it has been updated with the values from the source_array. The program then returns 0 which indicates that it runs successfully.
Demonstrating the Use of Memcpy() with a Struct
This code creates a structure called “Student” that has three elements: id, a 20-element character array and name, and a float value. The main function generates s1 and s2 student objects. The Id=1 and the names “Alice” and “3.5” are initialized on the first item which is s1. S2’s second item is blank. The content of s1 is subsequently copied into s2 using the memcpy() method.
The address of s2 and s1, as well as the size of the student, are sent to memcpy() in this manner. The members of s2 are then changed, with the id=2, the name=”Bob”, and gpa=3.7 being set. With the help of the cout statements, the code then prints the data for both students.
#include <cstring>
using namespace std;
struct Student {
int id;
char name[20];
float gpa;
};
int main() {
Student s1 = {1, "Alice", 3.5};
Student s2;
memcpy(&s2, &s1, sizeof(Student));
s2.id = 2;
strcpy(s2.name, "Bob");
s2.gpa = 3.7;
cout << "Student 1: " << s1.id << " " << s1.name << " " << s1.gpa << endl;
cout << "Student 2: " << s2.id << " " << s2.name << " " << s2.gpa << endl;
return 0;
}
The following is the output of the previoulsy-illustrated example:
Conclusion
We learned what the memcpy() function in the C++ programming language is and why we use the memcpy() function. We implemented some examples based on the memcpy() function so that we can understand the concepts.