Search code examples
cmemcpymemset

What is the difference between memset and memcpy in C


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


Solution

  • 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.