C String Functions - C memcpy

Copies n bytes between areas of memory.

void *memcpy(void *str1 , const void *str2 , size_t n );

Copies n characters from str2 to str1. If str1 and str2 overlap the behavior is undefined.

Returns the argument str1.


void * memcpy ( void * destination, const void * source, size_t num );


This function has the following parameter.

Pointer to the destination array to be copied, type-casted to a pointer of type void*.
Pointer to the source of data, type-casted to a pointer of type const void*.
Number of bytes to copy.

size_t is an unsigned integral type.


destination is returned.


#include <stdio.h>
#include <string.h>
//from   w  w  w  .j  a v a 2 s .  c  o m
struct {
  char name[40];
  int age;
} person, person_copy;

int main (){
  char myname[] = "this is a test";

  memcpy ( person.name, myname, strlen(myname)+1 );
  person.age = 46;

  memcpy ( &person_copy, &person, sizeof(person) );

  printf ("person_copy: %s, %d \n", person_copy.name, person_copy.age );

  return 0;

The code above generates the following result.