CKTIS / C
memcpy — C23 non-overlapping byte copy
`memcpy(s1, s2, n)` from `<string.h>` copies exactly `n` bytes of object representation from source `s2` to writable destination `s1` and returns the original value of `s1`. Bytes are interpreted as `unsigned char`; the function knows nothing about elements, array lengths, or text terminators.
Version: C23
Syntax
void *memcpy(void * restrict s1, const void * restrict s2, size_t n);memcpy(s1, s2, n) from <string.h> copies exactly n bytes of object representation from source s2 to writable destination s1 and returns the original value of s1. Bytes are interpreted as unsigned char; the function knows nothing about elements, array lengths, or text terminators.
The example copies the complete representation of a four-element int array into a separate array. Output 1 5 13 confirms return-pointer identity and the values actually copied into elements at indices 1 and 3.
The source and destination regions must not overlap even partly—such copying has undefined behavior; use memmove when they can overlap. Both pointer values must be valid even for n == 0, and for positive n the whole destination range must be writable and both ranges must fit their objects. memcpy is shallow: copied pointers still designate the same resources, no \0 is added, and a size computed as count * sizeof *p needs overflow checking.
Examples
#include <stdio.h>
#include <string.h>
int main(void){const int source[]={3,5,8,13};int target[4]={0};void *result=memcpy(target,source,sizeof source);printf("%d %d %d\n",result==target,target[1],target[3]);return 0;}
Result
1 5 13
The example copies the complete representation of a four-element `int` array into a separate array. Output `1 5 13` confirms return-pointer identity and the values actually copied into elements at indices 1 and 3.