On this page
memcpy
Prev: string.h | Up: string.h | Next: memmove
Rel: string.h, stdlib.h & string.h Guide
Ref: cppreference — memcpy
Tags: #public #prog #c #stdlib #string #tier/core
Include
#include <string.h>
Signature & Overview
void *memcpy(void *restrict dst, const void *restrict src, size_t n);
Copies n bytes from src to dst; undefined behavior if source and destination overlap.
Notes & Pitfalls
- Overlapping regions are Undefined Behavior: Use
memmove()wheneversrcanddstmight overlap; passingNULLeven withn == 0is UB in C99–C17.
Example
#include <assert.h>
#include <string.h>
int main(void) {
int src[2] = {10, 20}, dst[2] = {0};
memcpy(dst, src, sizeof(src));
assert(dst[0] == 10 && dst[1] == 20);
}
Linked from
Related notes
- tuts/c/stdlib/string/memmove Linked here; links to this page; shared tags: #c, #prog, #stdlib, #string, #tier
- tuts/c/stdlib/string Linked here; links to this page; shared tags: #c, #prog, #stdlib, #string
- tuts/c/stdlib/stdlib_and_string_h Linked here; shared tags: #c, #prog, #stdlib
- tuts/c/stdlib/string/memchr shared tags: #c, #prog, #stdlib, #string, #tier
- tuts/c/stdlib/string/memcmp shared tags: #c, #prog, #stdlib, #string, #tier
- tuts/c/stdlib/string/memset shared tags: #c, #prog, #stdlib, #string, #tier
- tuts/c/stdlib/string/strcat shared tags: #c, #prog, #stdlib, #string, #tier
- tuts/c/stdlib/string/strchr shared tags: #c, #prog, #stdlib, #string, #tier
- tuts/c/stdlib/string/strcmp shared tags: #c, #prog, #stdlib, #string, #tier
- tuts/c/stdlib/string/strcpy shared tags: #c, #prog, #stdlib, #string, #tier