memcpy Function

Copies characters from one buffer to another.

Include

<string.h>

Prototype

void *memcpy(void *dst, const void *src, size_t n);

Arguments

dst
buffer to copy characters to
src
buffer to copy characters from
n
number of characters to copy

Return Value

Returns dst.

Remarks

memcpy copies n characters from the source buffer src to the destination buffer dst. If the buffers overlap, the behavior is undefined.

For memcpy_eds, memcpy_packed, memcpy_p2d16 or memcpy_p2d1624, see “Functions for Specialized Copying and Initialization.”

Example

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

int main(void)
{
  char buf1[50] = "";
  char buf2[50] = "Where is the time?";
  char buf3[50] = "Why?";

  printf("buf1 : %s\n", buf1);
  printf("buf2 : %s\n", buf2);
  printf("buf3 : %s\n\n", buf3);

  memcpy(buf1, buf2, 6);
  printf("buf1 after memcpy of 6 chars of "
         "buf2: \n\t%s\n", buf1);

  printf("\n");

  memcpy(buf1, buf3, 5);
  printf("buf1 after memcpy of 5 chars of "
         "buf3: \n\t%s\n", buf1);
}

Example Output

buf1 :
buf2 : Where is the time?
buf3 : Why?

buf1 after memcpy of 6 chars of buf2:
        Where

buf1 after memcpy of 5 chars of buf3:
        Why?