Latest web development tutorials

C library functions - memcpy ()

C standard library - <string.h> C standard library - <string.h>

description

C library functionsvoid * memcpy (void * str1, const void * str2, size_t n) Copies ncharacters from the storage area to the storage areastr2str1.

statement

Here is () statement memcpy function.

void *memcpy(void *str1, const void *str2, size_t n)

parameter

  • str1 - point to the destination array for storing the copied contents, type cast to void * pointers.
  • str2 - pointing to copy the data source, type cast to void * pointers.
  • n - thenumber of bytes to be copied.

return value

This function returns a pointer to the destination store str1 pointer.

Examples

The following example demonstrates the memcpy () function is used.

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

int main ()
{
   const char src[50] = "http://www.w3cschool.cc";
   char dest[50];

   printf("Before memcpy dest = %s\n", dest);
   memcpy(dest, src, strlen(src)+1);
   printf("After memcpy dest = %s\n", dest);
   
   return(0);
}

Let's compile and run the above program, which will result in the following:

Before memcpy dest =
After memcpy dest = http://www.w3cschool.cc

C standard library - <string.h> C standard library - <string.h>