What is the difference between memset and memcpy in C

Dirk picture Dirk · Oct 8, 2009 · Viewed 37.1k times · Source

I've read the function headers, but I'm still not sure what exactly the difference is in terms of use cases.

Answer

Peter picture Peter · Oct 8, 2009

memcpy() copies from one place to another. memset() just sets all pieces of memory to the same value.

Example:

memset(str, '*', 50);   

The above line sets the first 50 characters of the string str to * (or whatever second argument of the memset).

memcpy(str2, str1, 50); 

The above line copies the first 50 characters of str1 to str2.