memcpy - C string.h

C examples for string.h:memcpy

From

<cstring> 
<string.h>

Description

Copy block of memory

Type

function

Prototype

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

Parameters

Parameter Description
destination Pointer to the destination array.
source Pointer to the source of data.
num Number of bytes to copy.

Return Value

destination is returned.

Demo Code


#include <stdio.h>
#include <string.h>

struct {/*from   w w w. ja v  a 2s .  co m*/
  char name[40];
  int age;
} person, person_copy;

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

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

  //copy structure
  memcpy ( &person_copy, &person, sizeof(person) );

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

  return 0;
}

Related Tutorials