cs24-20fa memcpy

Introduction to Computing Systems (Fall 2020)

Name

memcpy - copy memory area

Synopsis

#include <string.h>

void *memcpy(void *dest, const void *src, size_t n);

Description

The memcpy() function copies n byes from memory area src to memory area dest. The memory areas must not overlap: if they do, use memmove() instead.

Example

#include <assert.h>
#include <string.h>

int main() {
    char src[6] = "hello";
    char dest[6];

    // Copy six bytes/chars, five letters plus null character. 
    memcpy(dest, src, 6);
    assert(strcmp(src, dest) == 0)  
}

Return Value

Upon completion, the memcpy() function returns a pointer to dest.

See Also

memmove(), strcpy(), strncpy(), strcmp()