Javatpoint Logo
Javatpoint Logo

memmove() in C

The memmove() function transfers the memory block from one location to another. The function is declared in the string.h file.


The syntax for the memmove function is as follows:

Parameters Passed inside the Function

  • strng1: It is the pointer to the memory block where you wish to copy the contents of the source memory block. The datatype for the pointer is *void.
  • strng2: It is the pointer to the memory block from where you want to copy the contents to the destination memory block. The datatype for the pointer is *void.
  • n: It is the number of bytes of data to be moved from one location to another.


It returns a pointer pointing to the destination of the memory block, which according to the syntax, is strng1.

Implementation of memmove() function in C Programming

Let us implement the memmove() function. In this function, we will move the contents of the orgnl array to the new array.


memmove() in C

Some Facts related to the memmove() function in C programming:

Using examples, let us discuss some important facts related to the memmove() function.

  • As discussed earlier, the memmove() function in C Programming can move the content from the source location to the destination buffer even if both buffers overlap. If it is given that the source and destination are not overlapping with each other, then it is advised to use memcpy() instead of the memmove() function. But if they are overlapping, then the function will show undefined behavior. In that case, you should use the memmove() function.

Let us discuss the example:


memmove() in C
  • The execution time for both the functions that is memcpy() and memmove() is almost equal though it may differ with the platform or compiler. The memcpy() function can be slower or faster than the memmove(), but the difference is very small.


memmove() in C
  • The main difference between the working of the memmove() and memcpy() function is that the memmove() function is bidirectional while the memcpy() function is unidirectional. Memmove can also move the bytes in the forward and backward direction, while the memcpy can only move in the forward direction.

Implementing your memmove() function in C Programming

The standard function is defined in string.h header file is very efficient and accounts for all the possible cases the developer may face while moving the data from one location to another. So, if it is not necessary to define your memmove() function, the user should refrain from doing so.

Implementing the memmove() function is just like the memcpy function, but here we must take overlapping into account too. This makes the program more complex than the memcpy() function.

This also increases the space complexity of the program as we would require initializing a temporary array. The temporary array will be used to manage the overlapping scenario.

All the characters that are to be moved will be first stored in this temporary array then these characters stored in the temporary array will be moved to the destination location.

Let us code a driver program to ensure that the function built by us is working properly.


memmove() in C

In the above code, we have used the driver code. We have copied a string and array using the my_memove() function defined above. We have used a temporary array to ensure that overlapping is accounted for. Thus the string and array were copied, and our function worked perfectly.

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA