memcpy() in C
The memcpy() function is also called the Copy Memory Block function. It is used to make a copy of a specified range of characters. The function is only able to copy the objects from one memory block to another memory block if they both don't overlap at any point.
The syntax for memcpy() function in C language is as follows:
The memcpy() function will copy the n specified character from the source array or location. In this case, it is arr1 to the destination location that is arr2. Both arr1 and arr2 are the pointers that point to the source and destination location, respectively.
Parameter or Arguments passed in memcpy()
It returns a pointer that is the arr1.
Since the memcpy() function is defined in the string.h header file, it is necessary to include it in the code to implement the function.
Let us see how to implement the memcpy() function in C program.
Note: It is necessary to set the last index as null in the copied array as the function only copies the data and does not initialize the memory itself. The string expects a null value to terminate the string.
Important Facts to be Accounted before implementing memcpy() in C Programming:
The behavior of the code is not defined because the new pointer is not pointing to any valid location. Hence, the program will not function properly. In some compilers, it may also return an error. The destination pointer in the above case is invalid.
The output, in this case, is also similar to that in the above case, where the destination was not specified. The only difference here is it would not return any compilation error. It will just show undefined behavior as the source pointer is not pointing to any defined location.
Let us see some examples implementing the memcpy() function for different datatype of data.
Implementing the memcpy() function with char type data
Here we have initialized two arrays of size 30. The sourcearr contains the data to be copied into the destarr. We used the memcpy() function to store the data in destarr.
Implementing memcpy(0 function with integer type data
In this code, we have stored the integers in the array. Both the arrays can store int datatype. We have used the indexes to print the elements of the destarr after copying the elements of the sourcearr into destarr.
Implementing the memcpy() function with struct datatype
In the above code, we have defined the structure. We have used the memcpy() function twice. The first time we used it to copy the string into prsn1, we used it the second time to copy the data from the prsn1 to prsn2.
Define your memcpy() function in C Programming Language
Implementing the memcpy() function in the C Programming language is comparatively easy. The logic is quite simple behind the memcpy() function. To implement the memcpy() function, you must typecast the source address and the destination address to char*(1 byte). Once the typecasting is performed, now copy the contents from the source array to the destination address. We have to share the data byte by byte. Repeat this step until you have completed n units, where n is the specified bytes of the data to be copied.
Let us code our own memcpy() function:
Note: The function below works similarly to the actual memcpy() function, but many cases are still not accounted for in this user-defined function. Using your memcpy() function, you can decide specific conditions to be included in the function. But if the conditions are not specified, it is preferred to use the memcpy() function defined in the library function.
Let us write a driver code to check that above code is working properly on not.
Driver Code to test MemCpy() Function
In the code below we will use the arr1 to copy the data into the arr2 by using MemCpy() function.