C library function - memcpy()

Advertisements


Description

The C library function void *memcpy(void *str1, const void *str2, size_t n) copies n characters from memory area str2 to memory area str1.

Declaration

Following is the declaration for memcpy() function.

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

Parameters

  • str1 -- This is pointer to the destination array where the content is to be copied, type-casted to a pointer of type void*.

  • str2 -- This is pointer to the source of data to be copied, type-casted to a pointer of type void*.

  • n -- This is the number of bytes to be copied.

Return Value

This function returns a pointer to destination, which is str1.

Example

The following example shows the usage of memcpy() function.

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

int main ()
{
   const char src[50] = "http://www.tutorialspoint.com";
   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 us compile and run the above program, this will produce the following result:

Before memcpy dest =
After memcpy dest = http://www.tutorialspoint.com


Advertisements
Advertisements