Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

memcpy() in C/C++

  • Difficulty Level : Basic
  • Last Updated : 06 Sep, 2021

memcpy() is used to copy a block of memory from a location to another. It is declared in string.h

// Copies "numBytes" bytes from address "from" to address "to"
void * memcpy(void *to, const void *from, size_t numBytes);

Below is a sample C program to show working of memcpy().

Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for foundation plus STL.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.


/* A C program to demonstrate working of memcpy */
#include <stdio.h>
#include <string.h>
int main ()
  char str1[] = "Geeks"
  char str2[] = "Quiz"
  puts("str1 before memcpy ");
  /* Copies contents of str2 to str1 */
  memcpy (str1, str2, sizeof(str2));
  puts("\nstr1 after memcpy ");
  return 0;


str1 before memcpy 

str1 after memcpy 

1) memcpy() doesn’t check for overflow or \0 
2) memcpy() leads to problems when source and destination addresses overlap. 

memmove() is another library function that handles overlapping well.
Write your own memcpy() and memmove()
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!