memcpy in c

memcpy is a standard library function in C that is used to copy a block of memory from one location to another. Here is the syntax of the memcpy function:

void memcpy(void destination, const void *source, size_t num);
  • destination: Pointer to the destination array where the content is to be copied.
  • source: Pointer to the source of data to be copied.
  • num: Number of bytes to be copied.

The memcpy function copies the values of num bytes from the source to the destination. It returns a pointer to the destination array.

Here is an example usage of memcpy:

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

int main() {
   char source[] = "Hello, World!";
   char destination[20];

   memcpy(destination, source, strlen(source)+1);

   printf("Copied string: %s\n", destination);

   return 0;
}

In this example, the memcpy function is used to copy the contents of the source array to the destination array. The strlen(source)+1 is used to determine the number of bytes to be copied, ensuring that the null-terminating character is included. The copied string is then printed to the console.

Note: It is important to ensure that the destination array has enough space to accommodate the copied data.