The function void *memcpy(void *destination, const void *source, size_t n); copies first n bytes from memory location pointed by source to memory location pointed by destination. It does the binary copy of the data. It always copies exactly num bytes without checking for terminating null character('\0') in source.
Function prototype of memcpy
- destination : This is pointer to the memory location where the content is to be copied, type-casted to a pointer of type void*.
- source : This is pointer to the memory location from where data to be copied, type-casted to a pointer of type const void*.
- n : This is the number of bytes to copied.
Return value of memcpy
It returns a pointer to destination memory location.
C program using memcpy function
The following program shows the use of memcpy function to copy data from one string to another.
#include <stdio.h> #include <string.h> int main() { char source[100], destination[100]; printf("Enter source string\n"); gets(source); memcpy(destination, source, strlen(source)+1); printf("Destination string : %s\n", destination); return(0); }
Output
Enter source string TechCrashCourse Destination string : TechCrashCourse