×

memmove() in C

Introduction: In this article we are discuss about the memmove() function in C. This function transfers memory blocks from one location to another. The memmove() function is declared in the string.h header file.

Returns a pointer to the target. The memmove() feature works when objects overlap.

Syntax of the memmove() function in C:

The syntax of the memmove() function in C is given below –

void *memmove(void *str1, const void *str2, size_t n);  

Header file of the memmove() function in C:

The header file of the memmove() function in C is string.h. Which is written below –

#include <string.h>

Parameters of the memmove() function in C:

str1: str1 is a pointer to a memory block where the contents of the source memory block will be copied. The pointer data type is *void.

str2: str2 is a pointer to the memory block whose contents will be copied to the destination memory block. The pointer data type is *void.

n: Number of data bytes to move from one place to another.

Return value of the memmove() function in C:

Returns a pointer to the target of the memory block. According to the syntax, this is str1.

Example 1: Here, we implement of memmove() function in C. We have written a program below using this function –

#include <stdio.h>
#include <string.h>
int main ()
{
char dest [] = “Oldstring”;
const char src [] = “Newstring”;
printf (“Before the memmove dest = %s, src = %s\n”, dest, src);
memmove (dest, src, 9);
printf (“After the memmove dest = %s, src = %s\n”, dest, src);
return 0;
}

Result: We compile the above program and also run this program. After a successful compilation, the result is given below:

Before the memmove dest = Oldstring, src = Newstring
After the memmove dest = Newstring, src = Newstring

Example 2: Here, we implement of memmove() function in C. We have written another program below using this function –

#include <stdio.h>
#include <string.h>
int main ()
{
char str1 [] = “Priya”;
char str2 [] = “Biswas”;
puts (“str1 before memmove”);
puts (str1);
memmove (str1, str2, sizeof(str2));
puts (“\nstr1 after memmove”);
puts (str1);
return 0; }

Result: We compile the above program and also run this program. After a successful compilation, the result is given below:

Str1 before memmove
Priya
Str1 after memmove
Biswas

Example 3: Here, we implement the memmove() function in C. Using this function, we have written another program below –

#include <string.h>  
#include <stdio.h>  
#include <time.h>  
#define BUFFERSIZE  (100 * 1024)  
#define LOOP 10000  
int main()  
{  
 char destn[BUFFERSIZE] = {0};  
char source[BUFFERSIZE] = {0};  
 int i = 0;  
 double t_spent;  
clock_t start, end;  
start = clock();  
 for ( i = 0; i < LOOP; i++)  
 {  
memmove(destn,source,BUFFERSIZE);  
}  
end = clock();  
t_spent = (double)(end - start) / CLOCKS_PER_SEC;  
 printf("memmove() took %f seconds to complete\n", t_spent);
start = clock();  
for ( i = 0; i < LOOP; i++)  
 {  
memcpy(destn,source,BUFFERSIZE);  
 }  
  end = clock();  
t_spent = (double)(end - start) / CLOCKS_PER_SEC;  
printf("memcpy() took %f seconds to complete\n", t_spent);  
return 0;  
}  

Result: We compile the above program and also run this program. After a successful compilation the result is given below:

memmove() took 0.098000 seconds to complete
memcpy() took 0.083000 seconds to complete

What is the difference between memcpy() function and memmove() function in C?

The memcpy() function copies the data from one place to another. On the other hand, the memmove() function first copies the data to an intermediate buffer, then from the buffer to the destination. The memcpy() function causes problems with duplicate strings. The main difference in behaviour between the memmove() and memcpy() functions is that the memmove() function is bidirectional and the memcpy() function is unidirectional.

So, in this article, we clearly discuss the memmove() function in C. We discuss this function’s syntax, header file, parameters, and return value. We also give examples of the memmove() function in C and share the output of the programs.


Related Topics

OpenGL in C

OpenGL stands for Open Graphics Library. It is a low-level, widely supported modeling and rendering software package that is widely available across all the platforms. It is an API used...

4 minutes read.

How to open a C file on android mobile

A C file is a file that has a .c extension and contains code written in the C language. C is a computer programming language developed by Dennis Ritchie at...

4 minutes read.

Variable in C

Variable is an identifier that holds data in memory. It is used to identify input data in a program. The value of the variable can change at the time of...

2 minutes read.

Types of Array in C

What is an Array? One value can be stored in a variable at once. How many variables will you need if you have 100 values? Well, the answer isn't 100. It...

14 minutes read.

Type qualifiers in C

Type qualifiers in C: In the C programming language, type qualifiers are the keywords that prepend to the variables to change their accessibility, i.e., we can tell that the type...

4 minutes read.

clrscr in C

clrscr function in C clrscr stands for: clr = clear scr= screen When the clrscr() function is called in a program everything currently displayed in the console(output of previous programs, output of current program,...

3 minutes read.

Typecast vs. typedef in C

Typecast vs. typedef in C Typecast In the C programming language, converting the data type from one form to another is known as type casting or the type conversion. It is a...

5 minutes read.

Typedef vs define in C

Typedef VS define in C Typedef In the C programming language, a keyword called typedef can be used to give a type a new name. In other words, it is used to...

5 minutes read.

Const vs Volatile in C

Introduction Qualifiers are nothing but keywords which are used to modify the properties of a variable. Const and Volatile keywords are qualifiers in C. The Const qualifier is applied to the...

4 minutes read.

Bar3d() function in C Graphics

The bar3d function is used to create a 2-dimensional filled-in rectangular bar. We can also create three-dimensional shapes in C using helpful function. The first step in creating this 3D...

3 minutes read.

Ftell() Function in C

Ftell(): In File Handling we have some special functions like Ftell(), Fseek(), rewind() etc.. while you are randomly accessing the file these functions play very important role and these functions...

3 minutes read.

Fibonacci series program in C using Recursion

In this tutorial, we’ll explore how to utilise recursion to build the Fibonacci sequence in C language. What does the term "Fibonacci Series" mean? The following number in a Fibonacci number series is...

2 minutes read.

Nested Loops in C Programming Examples

A nested loop is generally used when we want to run a loop statement inside another loop statement. This kind of loop is also known as a “loop inside the...

6 minutes read.

Dos.h Header File in C Language

Dos.h is a header file in C. Interrupt handling, sound generation, date and time functions, and other tasks can be performed using the functions in this library. This is exclusive to...

4 minutes read.

How to delete a file in C

A file is a group of data kept on a secondary device, such as a hard disc. It is typically utilized as a real-world application with a lot of data. These...

3 minutes read.

Errors in C

Errors in C Errors are nothing but problems or faults that pretty much occur in all the programming languages. Errors make the behavior of the program seem abnormal, and even the...

4 minutes read.

Actual and Formal Parameters

Any variable declared within the parenthesis is referred to as the parameters during the function declaration. Parameters tell the function about the argument datatype, their order, and the number of...

5 minutes read.

C Program Swap Numbers in cyclic order Using Call by Reference

The three integers that the user entered in cyclical sequence are swapped in this tutorial using call by reference. C Program: #include <stdio.h> void cyclic_Swap ( int *X, int *Y, int *Z ); int...

2 minutes read.

CRC Program in C

CRC (Cyclic Redundancy Check) is an error-detection algorithm that is used to detect any errors that may have occurred during the transmission or storage of data. The basic idea behind...

4 minutes read.

Limitations of Inline Function in C

The compiler is unable to conduct inlining in two instances. It just accepts the inline definitions and produces space for the function in the same way it does for a...

3 minutes read.