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
| Parameter | Description |
|---|---|
destination | Pointer to the destination array where the content is to be copied, type-casted to void*. |
source | Pointer to the source of data to be copied, type-casted to const void*. |
num | Number 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:
- An array
sourcecontaining the string"Hello, World!"is defined. - An empty
destinationarray is created with enough space. - The
memcpy()function is used to copysizeof(source)bytes fromsourcetodestination, including the null character. - 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:
- An integer array
sourcewith values{1, 2, 3, 4, 5}is defined. - An empty
destinationarray is created. - The
memcpy()function is used to copysizeof(source)bytes. - 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:
- A
struct Personis defined with fieldsnameandage. - A structure variable
person1is initialized with values. - Another structure variable
person2is created. - The
memcpy()function is used to copyperson1intoperson2. - The copied values in
person2are printed.
Output:
Copied Person: Name = Alice, Age = 25
