memcpy() is used to copy a block of memory from a location to another. It is declared in string.h
Below is a sample C program to show working of memcpy().
/* A C program to demonstrate working of memcpy */ #include <stdio.h> #include <string.h> int main () { char str1[] = "Geeks"; char str2[] = "Quiz"; puts("str1 before memcpy "); puts(str1); /* Copies contents of str2 to sr1 */ memcpy (str1, str2, sizeof(str2)); puts("\nstr1 after memcpy "); puts(str1); return 0; }
Output:
str1 before memcpy
Geeks
str1 after memcpy
Quiz
Notes:
1) memcpy() doesn’t check for overflow or \0
2) memcpy() leads to problems when source and destination addresses overlap.
memmove() is another library function that handles overlapping well.