Copy characters between buffers.
#include <memory.h> | Required only for function declarations | |
#include <string.h> | Use either STRING.H (for ANSI compatibility) or MEMORY.H |
void *memcpy( void *dest, const void *src, size_t count );
void __far * __far _fmemcpy( void __far *dest, const void __far *src,
size_t count );
dest | New buffer | |
src | Buffer to copy from | |
count | Number of characters to copy |
The memcpy and _fmemcpy functions copy count bytes of src to dest. If the source and destination overlap, these functions do not ensure that the original source bytes in the overlapping region are copied before being overwritten. Use memmove to handle overlapping regions.
The _fmemcpy function is a model-independent (large-model) form of the memcpy function. It can be called from any point in any program.
There is a semantic difference between the function version of memcpy and its intrinsic version. The function version supports huge pointers in compact-, large-, and huge-model programs, but the intrinsic version does not.
The memcpy and _fmemcpy functions return the value of dest.
memcpy
Standards:ANSI, UNIX
16-Bit:DOS, QWIN, WIN, WIN DLL
32-Bit:DOS32X
_fmemcpy
Standards:None
16-Bit:DOS, QWIN, WIN, WIN DLL
32-Bit:None
_memccpy, memchr, memcmp, memmove, memset, strcpy, strncpy
/* MEMCPY.C. Illustrate overlapping copy: memmove handles it
* correctly; memcpy does not.
*/
#include <memory.h>
#include <string.h>
#include <stdio.h>
char string1[60] = "The quick brown dog jumps over the lazy fox";
char string2[60] = "The quick brown fox jumps over the lazy dog";
/* 1 2 3 4 5
* 12345678901234567890123456789012345678901234567890
*/
void main( void )
{
printf( "Function:\tmemcpy without overlap\n" );
printf( "Source:\t\t%s\n", string1 + 40 );
printf( "Destination:\t%s\n", string1 + 16 );
memcpy( string1 + 16, string1 + 40, 3 );
printf( "Result:\t\t%s\n", string1 );
printf( "Length:\t\t%d characters\n\n", strlen( string1 ) );
/* Restore string1 to original contents */
memcpy( string1 + 16, string2 + 40, 3 );
printf( "Function:\tmemmove with overlap\n" );
printf( "Source:\t\t%s\n", string2 + 4 );
printf( "Destination:\t%s\n", string2 + 10 );
memmove( string2 + 10, string2 + 4, 40 );
printf( "Result:\t\t%s\n", string2 );
printf( "Length:\t\t%d characters\n\n", strlen( string2 ) );
printf( "Function:\tmemcpy with overlap\n" );
printf( "Source:\t\t%s\n", string1 + 4 );
printf( "Destination:\t%s\n", string1 + 10 );
memcpy( string1 + 10, string1 + 4, 40 );
printf( "Result:\t\t%s\n", string1 );
printf( "Length:\t\t%d characters\n\n", strlen( string1 ) );
}
Function: memcpy without overlap
Source: fox
Destination: dog jumps over the lazy fox
Result: The quick brown fox jumps over the lazy fox
Length: 43 characters
Function: memmove with overlap
Source: quick brown fox jumps over the lazy dog
Destination: brown fox jumps over the lazy dog
Result: The quick quick brown fox jumps over the lazy dog
Length: 49 characters
Function: memcpy with overlap
Source: quick brown dog jumps over the lazy fox
Destination: brown dog jumps over the lazy fox
Result: The quick quick quick quick quick quick quick quick
Length: 50 characters