memcpy() Function

The memcpy() function in C is used to copy a block of memory from a source location to a destination. It performs a binary copy, meaning that it copies the exact number of bytes specified, without stopping for null characters (\0). This makes it useful for copying raw memory, structures, and arrays.


Syntax of memcpy()

</>
Copy
void *memcpy(void *destination, const void *source, size_t num);

Parameters

ParameterDescription
destinationPointer to the destination array where the content is to be copied, type-casted to void*.
sourcePointer to the source of data to be copied, type-casted to const void*.
numNumber of bytes to copy. size_t is an unsigned integral type.

Return Value

The function returns destination after copying num bytes from source.

Safety Note

Important: The source and destination memory blocks should not overlap. If they do, use memmove instead to avoid undefined behavior.


Examples for memcpy()

1. Copying an Array of Characters

This example demonstrates how to copy a string using memcpy():

Program

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

int main() {
    char source[] = "Hello, World!";
    char destination[20];

    // Copy 13 bytes from source to destination
    memcpy(destination, source, sizeof(source));

    printf("Copied String: %s\n", destination);

    return 0;
}

Explanation:

  1. An array source containing the string "Hello, World!" is defined.
  2. An empty destination array is created with enough space.
  3. The memcpy() function is used to copy sizeof(source) bytes from source to destination, including the null character.
  4. The copied string is printed.

Output:

Copied String: Hello, World!

2. Copying an Integer Array

This example demonstrates how to use memcpy() to copy an array of integers:

Program

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

int main() {
    int source[] = {1, 2, 3, 4, 5};
    int destination[5];

    // Copy the entire array
    memcpy(destination, source, sizeof(source));

    printf("Copied Array: ");
    for (int i = 0; i < 5; i++) {
        printf("%d ", destination[i]);
    }
    printf("\n");

    return 0;
}

Explanation:

  1. An integer array source with values {1, 2, 3, 4, 5} is defined.
  2. An empty destination array is created.
  3. The memcpy() function is used to copy sizeof(source) bytes.
  4. The copied array is printed using a loop.

Output:

Copied Array: 1 2 3 4 5

3. Copying a Structure

This example demonstrates how to use memcpy() to copy a structure:

Program

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

struct Person {
    char name[20];
    int age;
};

int main() {
    struct Person person1 = {"Alice", 25};
    struct Person person2;

    // Copy structure data
    memcpy(&person2, &person1, sizeof(person1));

    printf("Copied Person: Name = %s, Age = %d\n", person2.name, person2.age);

    return 0;
}

Explanation:

  1. A struct Person is defined with fields name and age.
  2. A structure variable person1 is initialized with values.
  3. Another structure variable person2 is created.
  4. The memcpy() function is used to copy person1 into person2.
  5. The copied values in person2 are printed.

Output:

Copied Person: Name = Alice, Age = 25