Name

memcpy

Synopsis

Copies the contents of a memory block

#include <string.h>
void *memcpy( void * restrict dest, const void * restrict src, size_t n );

The memcpy() function copies n successive bytes beginning at the address in src to the location beginning at the address in dest. The return value is the same as the first argument, dest. The two pointer values must be at least n bytes apart, so that the source and destination blocks do not overlap; otherwise, the function’s behavior is undefined. For overlapping blocks, use memmove().

Example

typedef struct record {
  char  name[32];
  double data;
  struct record *next, *prev;
} Rec_t;

Rec_t template = { "Another fine product", -0.0, NULL, NULL };
Rec_t *tmp_new;

if (( tmp_new  = malloc( sizeof(Rec_t) )) != NULL )memcpy( tmp_new, &template, sizeof(Rec_t) );
else
  fprintf( stderr, "Out of memory!\n" ), return -1;

See Also

strcpy(), strncpy(), memove(), wmemcpy(), wmemmove()

Get C in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.