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.

Prototype

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

Parameter

This function has the following parameter.

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

size_t is an unsigned integral type.

Return

destination is returned.

Example


#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.