memcpy() — Copy buffer

Standards

Standards / Extensions C or C++ Dependencies
ISO C
XPG4
XPG4.2
C99
Single UNIX Specification, Version 3
both  

Format

#include <string.h>

void *memcpy(void * __restrict__dest, const void * __restrict__src, size_t count);

General description

The memcpy() built-in function copies count bytes from the object pointed to by src to the object pointed to by dest. See Built-in functions for information about the use of built-in functions. For memcpy(), the source characters may be overlaid if copying takes place between objects that overlap. Use the memmove() function to allow copying between objects that overlap.

Returned value

memcpy() returns the value of dest.

Example

CELEBM13
/* CELEBM13   
                                   
   This example copies the contents of source to target.                        
                                                                                
 */                                                                             
#include <string.h>                                                             
#include <stdio.h>                                                              
                                                                                
#define MAX_LEN 80                                                              
                                                                                
char source[ MAX_LEN ] = "This is the source string";                           
char target[ MAX_LEN ] = "This is the target string";                           
                                                                                
int main(void)                                                                  
{                                                                               
  printf( "Before memcpy, target is \"%s\"\n", target );                        
  memcpy( target, source, sizeof(source));                                      
  printf( "After memcpy, target becomes \"%s\"\n", target );                    
}                                                                               
Output
Before memcpy, target is "This is the target string"
After memcpy, target becomes "This is the source string"

Related information