Memcpy function in c – memcpy C Library Function

Memcpy function in c: 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

Memcpy c example: It returns a pointer to destination memory location.

C program using memcpy function

Memcpy in c: The following program shows the use of memcpy function to copy data from one string to another.

memcpy C Library Function

#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);
}

Output

Enter source string
TechCrashCourse
Destination string : TechCrashCourse