#include <string.h> void *memcpy(void *to, const void *from, size_t n);
memcpy
copies the number of bytes specified by n
from one area
of memory (from
) to another (to
). All bytes, including any
null characters, are copied.
memcpy
returns a pointer to the to
area.
memcpy
when the source and target fields overlap is
undefined. Sometimes a run-time diagnostic message is produced in this
case.
The third argument to memcpy
is size_t
. If a negative number
is passed, overlaying of memory may occur.
memcpy
unless memcpy
is
undefined (by an #undef
statement) to prevent this. The inline
code may still call a library routine in special cases (for example, if
the length is a variable whose value is larger than 16 megabytes).
The code generated for memcpy
usually uses the MVCL instruction to
perform data movement. If more than 16 megabytes of data are to be
moved, the library routine is called, which moves 16M-1 bytes at a time.
(Thus, the effect on overlapping fields can depend on whether they are
separated by as much as 16 megabytes.) For more information on
optimizing your use of memcpy
, see Optimizing Your Use of memcmp, memcpy, and memset .
#include <string.h> #include <stdio.h> #define TAILSIZE 10 main() { char buf[160]; char tail[TAILSIZE+1]; puts("Enter a line of text."); gets(buf); if (strlen(buf) < TAILSIZE) printf("Your input was shorter than %d characters.n", TAILSIZE); else{ memcpy(tail, buf+strlen(buf)-TAILSIZE, TAILSIZE+1); /* Copy last 10 characters of buf, */ /* plus the trailing null. */ printf("The last 10 characters of your input were " ""%s".n", tail); } }
memcpyp
, memmove
, strcpy
, strncpy