C memmove() function
C memmove() function - copy bytes in memory with overlapping areas
The memmove() function is used to copy n bytes of src to dest.
Use memmove() function when:
- You need to copy data within the same memory block, particularly if there’s a possibility of overlapping regions, such as shifting data in an array.
- Making in-place modifications to an array or buffer, allowing part of the array to overwrite another part without losing data.
- Adjusting sections within a structure or array where safe handling of overlapping regions is essential.
Syntax:
void *memmove(void *src, const void *dest, size_t n)
Parameters:
Name | Description | Required /Optional |
---|---|---|
dest | Destination object. | Required |
src | Source object. | Required |
n | Number of bytes. | Optional |
Return value from memviod()
The memmove() function shall return dest. No return value is reserved to indicate an error.
Examples: memmove () function
Example 1: Copy a specific part of a string within the same memory block
Following memmove() example demonstrates how to copy a specific part of a string within the same memory block. It sets up a pointer to the 8th position in the target string, where the word "brown" begins, and another pointer to the 2nd position. Using memmove(), it copies five characters from position 2 to position 8, effectively replacing "brown" with "e qui". This example highlights memmove()'s ability to safely handle overlapping memory areas without corrupting data, making it useful for in-place modifications.
Code:
#include <string.h>
#include <stdio.h>
#define SIZE 45
char target[SIZE] = "The quick brown fox jumps over the lazy dog.";
int main( void )
{
char * p = target + 8; /* p points at the starting character
of the word we want to replace */
char * source = target + 2;
printf( "Before memmove, target is \"%s\"\n", target );
memmove( p, source, 5 );
printf( "After memmove, target becomes \"%s\"\n", target );
}
Output:
Before memmove, target is "The quick brown fox jumps over the lazy dog." After memmove, target becomes "The quice quiwn fox jumps over the lazy dog."
Example 2: Copy data from one location to another safely
This example demonstrates the use of the memmove() function to copy data from one location to another safely, even if the source and target memory regions overlap. Here, memmove() copies the contents of str1 to str2.
#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 memmove, strings are:");
printf("\n%s",str1);
printf("\n%s",str2);
memmove(str2, str1, sizeof(str1));
printf( "\n\nAfter memmove, strings are:");
printf("\n%s",str1);
printf("\n%s",str2);
}
Output:
Before memmove, strings are: This is the string1 This is the string2 After memmove, strings are: This is the string1 This is the string1
Example 3: Shifting elements in an array
This example demonstrates using memmove() to shift elements in an integer array by one position.
Code:
#include <stdio.h>
#include <string.h>
int main() {
// Define an array of integers
int arr[] = {1, 2, 3, 4, 5, 6};
int size = sizeof(arr) / sizeof(arr[0]);
// Shift elements in the array one position to the right
memmove(&arr[1], &arr[0], (size - 1) * sizeof(int));
// Update the first element
arr[0] = 0;
// Display the modified array
printf("Shifted array: ");
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
return 0;
}
Output:
Shifted array: 0 1 2 3 4 5
Explanation:
- An integer array is defined and initialized.
- memmove() shifts elements one position to the right by moving data from arr[0] to arr[1].
- The first element is set to 0, and the modified array is printed, showing the shift.
Example 4: Overwriting Part of a String
This example demonstrates replacing a portion of a string using memmove().
Code:
#include <stdio.h>
#include <string.h>
int main() {
// Define a string
char text[] = "Hello, wonderful world!";
char *src = text + 7; // Points to "wonderful"
char *dest = text + 0; // Points to the beginning of the string
// Copy "wonderful" to the beginning of the string
memmove(dest, src, 9);
// Display the modified string
printf("Modified text: %s\n", text);
return 0;
}
Output:
Modified text: wonderfulnderful world!
Explanation:
- A string text is defined with the phrase "Hello, wonderful world!".
- Pointers are set to copy "wonderful" over "Hello," at the beginning of text.
- memmove() safely copies "wonderful" to the beginning, resulting in "wonderful world!".
C Programming Code Editor:
Previous C Programming: C memcpy()
Next C Programming: C memset()
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://w3resource.com/c-programming/string/c-memmove.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics