×

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

Comma Operator in C

What is a comma operator? The comma operator in the C programming language has the least priority. The comma operator is essentially a binary operator that operates on the first operand...

4 minutes read.

Multiplication Table in C

Displaying table up to 10 #include <stdio.h> int main() {     int num, i = 1;     printf("     Enter any Number:");     scanf("%d", &num);     printf("Multiplication table of %d: ", num);  ...

1 minute read.

Flexible Array Members in a Structure in C

There is flexibility to declare array from c99 generation onwards that declaration of the collections can be made possible without even mentioning its dimension, which means that the displays are...

4 minutes read.

Operators in C

An operator is a special symbol that tells the compiler to perform specific mathematical or logical operations. It is the combination of constants and variables through expressions. Example: int c = a+b*5 Where,...

5 minutes read.

Assert() Function in C

Assert (): In C, the statements are executed with the exit statement. In C language declare, and it tests the condition parameters. If the statement executed will be false, it shows...

4 minutes read.

C Data type

Data Types in C with Examples Data types are used to define the type of data that a variable can store to perform a specific operation.ANSI C provides three types of...

2 minutes read.

How to use Typedef Struct in C

The “typedef” is a predefined keyword in C programming language which is used to declare the new name to an existing type of variable. For better understanding, if you declare a...

3 minutes read.

Multiplication table program in C using For loop

Before we move on the program of multiplication table of any natural number, first we have to know about the For loop statement. The syntax of ‘for’ loop in C programming...

3 minutes read.

Addition of Matrix in C

The mathematical operation of includingor greater matrices is called the addition of matrices. A matrix is a square series of numbers, symbols, words, letters, and different things. In this article,...

3 minutes read.

Sum of N numbers in C using For loop

Before we move on the program of sum of N numbers, first we have to know about the For Loop statement. The syntax of ‘for’ loop in C programming language is...

3 minutes read.

Deadlock Detection Program in C

What is Deadlock? A deadlock is a circumstance where a group in the process is halted because they are each holding onto resources while waiting for other methods to obtain them. Think...

5 minutes read.

Find occurrence of substring in C using function

Introduction: In this article, we discuss finding the occurrence of a substring in C using function. This software takes strings and substrings as input and counts the occurrences of substrings within...

3 minutes read.

strcat() Function in C

Strings in c: A string is defined as the set of characters that are enclosed within the double quotations (" "). The String always ends with the null character ("\0"). The...

3 minutes read.

GCD program of two numbers in C

GCD stands for Greatest Common Divisor, which is also known as Highest Common Factor, which can be abbreviated as HCF. Mathematically it can be defined as any two positive integers...

3 minutes read.

Use of free() function in C

Introduction The free() function uses in C programming language. The free() function in the C programming language uses to release or deallocate the memory blocks; these blocks are previously allocated by calloc(), malloc() or realloc()...

3 minutes read.

Data Types in C

Data type is a very important concept in C programming language. Simply, “A data type is the classification of data values that a data item can have.” We need to...

11 minutes read.

Merge Sort in C

The merge sort follows the principle of the divide and conquers algorithm. Firstly it divides the input of an array into two halves and then calls itself for the two...

4 minutes read.

Difference between C and Java

C programming and Java programming are two of the earliest programming languages. C programming follows a procedural approach whereas Java programming follows an object-oriented approach. Java programming is a part...

3 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.

String Handling functions in C

String :- The String is the collection of characters. Every String ends with the null character, and the String is enclosed in the double quotations .ie, "javaTpoint". If we see any...

5 minutes read.