copy memory area

You're viewing an older version of this page (#4058). View the current version.

View versions (3)

INTERFACE

#include <string.h>
void memcpy ( void *dst, const void *src, size_t len )

DESCRIPTION

The memcpy function copies len bytes from memory area src to memory area dst. If src and dst overlap, behavior is undefined. Applications in which src and dst might overlap should use memmove instead.

Example 1
Workings

\code #include <stdio.h> #include <string.h> int main() { // define two arrays u, v and initialize u int u[5] = {1, 2, 3, 4, 5}, v[5]; // copy u to v memcpy(v, u, 5*sizeof(int)); // display v for (int i = 0; i < 5; ++i) printf("v[%d] = %d
", i, v[i]); return 0; } \endcode

Solution

Output:

v[0] = 1
v[1] = 2
v[2] = 3
v[3] = 4
v[4] = 5

RETURN VALUES

The memcpy function returns the original value of dst.

SEE ALSO

memmove, strcpy