memcpy : <string.h> library function

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

void *memcpy (void *destination, const void *source, size_t n);
  • 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 to show the use of 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>
#include <conio.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);
   
   getch();
   return(0);
}

Program Output
Enter source string
TechCrashCourse
Destination string : TechCrashCourse