Javatpoint Logo
Javatpoint Logo

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.

Syntax

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()

  • arr1: it is the first parameter in the function that specifies the location of the source memory block. It represents the array that will be copied to the destination.
  • arr2: The second parameter in the function specifies the location of the destination memory block. It represents the array where the memory block will be copied.
  • n: It specifies the number of characters copied from source to destination.

Return

It returns a pointer that is the arr1.

Header file

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 memcpy() function is declared in the string.h header file. So the programmer needs to ensure to include the file in the code.
  • The size of the buffer in which the content is to be copied must be greater than the number of bytes to be copied into the buffer.
  • It does not work when the objects overlap. The behavior is undefined if we try to perform the function on the objects that overlap.
  • It is necessary to add a null character when using the strings as it does not check for the terminating null characters in the strings.
  • The function behavior will not be defined if the function will access the buffer beyond its size of it. It is better to check the buffer size using the sizeof() function.
  • It does not ensure that the destination memory block is valid in the memory of the system or not.

Output:

memcpy() in C

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 memcpy() function also does not perform the validation of the source buffer.

Output:

memcpy() in C

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.

  • The memcpy() functions work on the byte level of the data. Therefore the value of n should always be in bytes for desired results.
  • In the syntax for the memcpy() function, the pointers are declared void * for both the source and the destination memory block, which means that they can be used to point toward any type of data.

Let us see some examples implementing the memcpy() function for different datatype of data.

Implementing the memcpy() function with char type data

Output:

memcpy() in C

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

Output:

memcpy() in C

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

Output:

memcpy() in C

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.

Output:

memcpy() in C
Next Topicmemmove() in C





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA