C library - memmove() function



The C library memchr() function is used to copy a block memory from one location to another. Typically, this function state the count bytes of data from a source location to the destination.

Syntax

Following is the syntax of the C library memchr() method −

void *memmove(void *dest_str, const void *src_str, size_t numBytes)

Parameters

This function accepts the following parameters

  • *dest_str − This is a pointer for destination array where the content is to be copied. It is type-casted to a pointer of type void*.

  • *src_str − The is a second pointer that denote the source of data to be copied. It is also type-casted to a pointer of type void*.

  • numBytes − This parameter refer to number of bytes to be copied.

Return Value

This function returns a pointer to the destination i.e *dest_str.

Example 1

Following is the basic C library function memchr() to change the postion of strings.

#include <stdio.h>
#include <string.h>

int main () {
   char dest_str[] = "oldstring";
   const char src_str[]  = "newstring";
   printf("Before memmove dest = %s, src = %s\n", dest_str, src_str);
   memmove(dest_str, src_str, 9);
   printf("After memmove dest = %s, src = %s\n", dest_str, src_str);
   return(0);
}

Output

The above code produces the following output−

Before memmove dest = oldstring, src = newstring
After memmove dest = newstring, src = newstring

Example 2

In the below example, we use the puts() method for the destination string. This method add the string with a newline character and returns an integer value. When copying the content from the source string to the destination, we utilize the memmove() method.

#include <stdio.h> 
#include <string.h> 

int main() 
{ 
	char dest_str[] = "Tutorialspoint"; 
	char src_str[] = "Tutors";

	puts("source string [src_str] before memmove:-"); 
	puts(dest_str); 

	/* Copies contents from source to destination */
	memmove(dest_str, src_str, sizeof(src_str)); 

	puts("\nsource string [src_str] after memmove:-"); 
	puts(dest_str); 
	return 0; 
}

Output

On executing the above code, we get the following output−

Source string [src_str] before memmove:-
Tutorialspoint

Source string [src_str] after memmove:-
Tutors
Advertisements