C memcpy() and memmove()
Remove a prefix with memmove when source and destination ranges overlap.
How it works
memcpy requires non-overlapping source and destination regions. memmove supports overlap and behaves as though the source bytes were copied through a temporary array. Both functions operate on exact byte counts and do not know about strings, logical records or destination capacity. When moving a terminated string suffix, include its terminator. For structures with owning pointers, moving raw representations can also complicate ownership unless the collection design explicitly transfers each object rather than leaving duplicate live owners.
Run it locally
Use a hosted C17 compiler in a separate lab folder. The command assumes a driver named cc; substitute gcc, clang or zig cc as appropriate. C does not currently run in Bhylu’s browser playground. Apache, PHP and MySQL are not needed for these C programs.
cc -std=c17 -Wall -Wextra -Wpedantic -Werror buffer-moves.c -o lessonOn Windows, use -o lesson.exe and run .\lesson.exe in PowerShell. On Unix-like systems run ./lesson. Math-library linking differs by toolchain; GCC on many Unix systems needs -lm after the source.
Download the complete C source. File-writing labs must run in an empty private directory, away from existing reports.
Example
#include <stdio.h>
#include <string.h>
int main(void) {
char label[] = "XXRidge";
size_t remaining = strlen(label + 2) + 1;
memmove(label, label + 2, remaining);
printf("Trimmed: %s\n", label);
return 0;
}
Expected result
Trimmed: Ridge
The suffix begins two bytes into the same array, so the regions overlap. Its length plus one includes the terminating byte. The remaining capacity is sufficient because the move shortens the current string. For outside data, first verify that an accessible terminator exists within a known bound; an unbounded strlen is not an input validation strategy.
For language and library rules, consult the WG14 C working group and its public C11 committee draft, which describes the core facilities used here; C17 incorporates corrections. Check your compiler and runtime documentation for implementation details.