C library - memcpy() function



The C library memcpy() function is also known as Copy Memory Block function / Memomy to Memory Copy. It is used to specify the range of characters which could not exceed the size of the source memory.

Syntax

Following is the syntax of the C library memcpy() function −

void *memcpy(void *dest_str, const void * src_str, size_t n)

Parameters

This function accepts the following parameters−

  • dest_str − This parameter define a pointer to the destination array where the content is to be copied. It is type-casted to a pointer of type void*.

  • src_str − This parameter is used to define the source of data to be copied. It is then type-casted to a pointer of type void*.

  • n − This parameter refer to number of bytes to be copied.

Return Value

This function returns a pointer to destination i.e. dest_str.

Example 1

The C library function memcpy() uses three parameters− destination string(dest), source string(src), and strlen() function where it calculates the length of the source string and the number of bytes to be copied.

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

int main ()
{
   const char src[50] = "Tutorialspoint";
   char dest[50];
   strcpy(dest,"Heloooo!!");
   printf("Before memcpy dest = %s\n", dest);
   memcpy(dest, src, strlen(src) + 1);
   printf("After memcpy dest = %s\n", dest);
   return(0);
}

Output

The above code produces following result−

Before memcpy dest = Heloooo!!
After memcpy dest = Tutorialspoint

Example 2

Below the program uses two functions− puts() and memcpy() to copy the content from one memory address/location to another.

#include <stdio.h>
#include <string.h>
int main() 
{
    char first_str[] = "Tutorials";
    char sec_str[] = "Point";

    puts("first_str before memcpy:");
    puts(first_str);

    // Copy the content of first_str to sec_str
    memcpy(first_str, sec_str, sizeof(sec_str));

    puts("\nfirst_str after memcpy:");
    puts(first_str);

    return 0;
}

Output

On execution of above code, we get the following result−

first_str before memcpy:
Tutorials

first_str after memcpy:
Point

Example

Following is the C program that demonstrate the code snippet of memcpy() function to represent the text before and after using certain action or process.

#include <stdio.h>
#include <string.h>
#define MAX_CHAR 100
int main() 
{
	char first_str[MAX_CHAR] = "Hello World!";
	char second_str[MAX_CHAR] = "Welcome to Tutorialspoint";

	printf("The Actual Statements:-\n");
	printf("first_str: %s\n", first_str);
	printf("second_str: %s\n", second_str);

	//copying all bytes of second_str to first_str
	memcpy(first_str, second_str, strlen(second_str));

	printf("After executing the function memcpy()\n");
	printf("first_str: %s\n", first_str);
	printf("second_str: %s\n", second_str);
	return 0;
}

Output

After executing the code, we get the following result−

The Actual Statements:-
first_str: Hello World!
second_str: Welcome to Tutorialspoint
After executing the function memcpy()
first_str: Welcome to Tutorialspoint
second_str: Welcome to Tutorialspoint
Advertisements