C memcpy() function
C memcpy() function - copy bytes in memory
The memcpy() function is used to copy n bytes from the object pointed to by s2 into the object pointed to by s1. If copying takes place between objects that overlap, the behavior is undefined.
Use memcpy() when:
- Copying Fixed-Length Data: You need to transfer a known number of bytes from one memory location to another.
- Working with Non-String Data: It’s ideal for copying structs, arrays of integers, or other non-string data.
- Performance: Since it doesn't check for null characters, memcpy() can be faster for large data transfers.
Syntax:
void *memcpy(void *s1, const void * s2, size_t n)
Parameters:
Name | Description | Required /Optional |
---|---|---|
s1 | New buffer. | Required |
s2 | Buffer to copy from. | Required |
n | Number of characters to copy. | Optional |
Return value from memcpy()
The memcpy() function shall return s. No return value is reserved to indicate an error.
Examples: C memcpy() function
Example 1: Copying part of a string over itself
This example demonstrates memcpy() by copying part of a string over itself. Starting from index 3, it copies 5 characters from the beginning, which results in an overlapping copy within the same string.
Code:
#include <stdio.h>
#include <string.h>
char str1[9] = "AAABBCCD";
int main( void )
{
printf( "Original string: %s\n", str1 );
memcpy( str1 + 3, str1, 5 );
printf( "New string: %s\n", str1 );
}
Output:
Original string: AAABBCCD New string: AAAAAABB
Example 2: How memcpy() is used to copy the entire contents of str1 into str2?
This example shows how memcpy() is used to copy the entire contents of str1 into str2. After copying, str2 becomes an identical copy of str1.
#include <string.h>
#include <stdio.h>
#define MAX_LEN 80
char str1[ MAX_LEN ] = "This is the string1";
char str2[ MAX_LEN ] = "This is the string2";
int main(void)
{
printf("Before memcpy, strings are:");
printf("\n%s",str1);
printf("\n%s",str2);
memcpy(str2, str1, sizeof(str1));
printf( "\n\nAfter memcpy, strings are:");
printf("\n%s",str1);
printf("\n%s",str2);
}
Output:
Before memcpy, strings are: This is the string1 This is the string2 After memcpy, strings are: This is the string1 This is the string1
Example 3: Copying array data using memcpy()
This example demonstrates using memcpy() to copy data from one integer array to another.
Code:
#include <stdio.h>
#include <string.h>
int main() {
// Define and initialize two integer arrays
int source[5] = {1, 2, 3, 4, 5};
int destination[5];
// Copy contents of source array to destination array
memcpy(destination, source, sizeof(source));
// Display the destination array contents
printf("Copied array: ");
for(int i = 0; i < 5; i++) {
printf("%d ", destination[i]);
}
return 0;
}
Output:
Copied array: 1 2 3 4 5
Explanation:
- The program defines a source array of integers and a destination array.
- memcpy() copies the entire source array into the destination array.
- Finally, the destination array contents are printed to verify the copy.
Example 4: Copying Part of a String with memcpy()
In this example, memcpy() copies part of a string from one location in memory to another in the same string.
Code:
#include <stdio.h>
#include <string.h>
int main() {
// Define a source string
char text[50] = "Hello, World!";
// Copy "World" to the beginning of the string
memcpy(text, text + 7, 5);
// Null-terminate the modified string
text[5] = '\0';
// Display the modified string
printf("Modified string: %s\n", text);
return 0;
}
Output:
Modified string: World
Explanation:
- The program initializes a string containing "Hello, World!".
- memcpy() copies 5 bytes starting from "World" (index 7) to the beginning of text.
- The copied section is then null-terminated to form a valid string, resulting in "World" being printed.
C Programming Code Editor:
Previous C Programming: C memcmp()
Next C Programming: C memmove()
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://www.w3resource.com/c-programming/string/c-memcpy.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics