C programming memmove string.h


In C programming the <string.h> memmove function copies a specific number of characters from one memory block to another.The declaration of the function is given below.

void *memmove(void *dst, const void *src, size_t n);

Parameters:
dst -The pointer to memory block where the characters is to be copied.

src -The pointer to memory block from where the characters are to be taken for copying to ‘dst’ memory block.

n -The number of characters to be copied.

Return type
void* -Returns the value copied to ‘dst’.

Note this function behaved as though ‘n’ number of characters is copied first to a temporary array which is then copied again to the destination memory block.For this function the source and destination memory block may overlap.A code example is given below.

Code example

char a[] = “The new string” ;

memmove( a+4 , a+8 , 7);

printf(“%s”, a);

Output,

The string

If you are confused with the working of the program here is a brief explanation:
The destination pointer is ‘a+4’ which is the fifth block in the ‘a’ array,and the source pointer is the ‘a+8’ which is the ninth block of the ‘a’ array,since the source pointer points to ‘a+8’ the ninth and the remaining characters are to be copied to the ‘a+4’ and the remaining block of the ‘a’ array.Note here the source and destination memory overlap but there is no problem here!


Related links

->C memcpy string.h

->C strcpy string.h

->C strncpy string.h