The memcpy function signature
What you'll learn: How to interpret the generic nature of memcpy and why it uses void* to move raw memory between any two data types.
The universal photocopier
Imagine you have a high-end office photocopier. It doesn’t care if you are scanning a birth certificate, a drawing of a cat, or a secret recipe. To the machine, everything is just a sheet of paper. It simply looks at the ink on one page and replicates it onto another.
In C, the memcpy function is that photocopier. Its job is to copy a specific number of bytes from one memory location to another, regardless of what those bytes actually represent (integers, floats, or custom structures).
Breaking down the signature
To achieve this "universal" behavior, memcpy uses void pointers. Here is how it is defined in the <string.h> library:
void *memcpy(void *dest, const void *src, size_t n);
Let’s look at the three parts of this signature:
void *dest: This is the destination address. Because it is avoidpointer, you can pass an address to any type of data here. It is the "blank paper" where the data will be written.const void *src: This is the source address. Theconstkeyword is a promise thatmemcpywill only read from this memory, not change it. Again, being avoid*allows it to point to anything.size_t n: This is the most important part. Sincevoidpointers have no size (the computer doesn't know if they point to a 4-byteintor a 400-bytestruct), you must explicitly state exactly how many bytes you want to copy.
Why it matters
If memcpy were defined using int*, you would have to typecast every time you wanted to copy a double or a char. By using void*, C allows the function to operate on raw memory addresses. It treats your data as a simple sequence of bytes, ignoring the "type" entirely. This makes memcpy one of the most powerful tools for generic programming, allowing you to move complex data structures with a single line of code.
Key takeaway
The memcpy signature uses void* to remain "type-blind," allowing it to copy any data structure as long as you provide the exact byte count.