memcpy() - Buffer Manipulation Function

How is "memcpy()" used in C++?


memcpy() buffer manipulation copies the "count" characters from the array block, "str2" to str1". When the arrays overlap behaviour of this function is undefined, this also returns a pointer to "str1". This function simply copies byte by byte characters of the block from the array.


void *memcpy(void *str1, void *str2, size_t count);

Example :

#include <stdio.h>
#include <string.h>
int main ()
char str1[40];
char str2[]="Memory copy string";
memcpy (str1,str2,strlen(str2)+1);
printf ("Array str1 is: %s\nArray
str2 is: %s\n",str1,str2);
return 0;

Result :

Array str1 is:Memory copy string
Array str2 is:Memory copy string

In the above example, this buffer manipulation is used to copy block of contents of "str2" to "str1", the count is same as the length of "str1".

C++ Tutorial

Ask Questions

Ask Question