Example of memcpy function

Example of memcpy function

Memcpy is used to copy memory blocks

The syntax of memcpy function:

void * memcpy ( void * destination, const void * source, size_t num );
Sour‮www:ec‬.lautturi.com

Copy the value of num bytes directly from the location pointed to by the source to the memory block pointed to by the target.
The underlying type of the object pointed to by the source pointer and target pointer is independent of this function;
The result is a binary copy of the data.
This function does not check for any terminating null characters in the source code - it always copies num bytes exactly.
To avoid overflow, the size of the array pointed to by the target and source parameters should be at least num bytes and should not overlap (memmove is a safer method for overlapping memory blocks).

#include <stdio.h>
#include <string.h>

int main () {
   const char src[50] = "lautturi";
   char dest[50];
   strcpy(dest,"lautturi world!!");
   printf("Before memcpy dest = %s\n", dest);
   memcpy(dest, src, strlen(src)+1);
   printf("After memcpy dest = %s\n", dest);
   
   return(0);
}
Created Time:2017-08-29 09:32:32  Author:lautturi