memcpy() function in C/C++


The function memcpy() is used to copy a memory block from one location to another. One is source and another is destination pointed by the pointer. This is declared in “string.h” header file in C language. It does not check overflow.

Here is the syntax of memcpy() in C language,

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

Here,

dest_str − Pointer to the destination array.

src_str − Pointer to the source array.

number − The number of bytes to be copied from source to destination.

Here is an example of memcpy() in C language,

Example

 Live Demo

#include <stdio.h>
#include <string.h>
int main () {
   char a[] = "Firststring";
   const char b[] = "Secondstring";
   memcpy(a, b, 5);
   printf("New arrays : %s\t%s", a, b);
   return 0;
}

Output

New arrays : SeconstringSecondstring

In the above program, two char type arrays are initialized and memcpy() function is copying the source string ‘b’ to the destination string ‘a’.

char a[] = "Firststring";
const char b[] = "Secondstring";
memcpy(a, b, 5);

Samual Sam
Samual Sam

Learning faster. Every day.

Updated on: 26-Jun-2020

5K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements