memcpy
From cppreference.com
Defined in header
<string.h>
|
||
void* memcpy( void *dest, const void *src, size_t count );
|
(until C99) | |
void* memcpy( void *restrict dest, const void *restrict src, size_t count );
|
(since C99) | |
Copies count
characters from the object pointed to by src
to the object pointed to by dest
. If the objects overlap, the behavior is undefined.
Contents |
[edit] Parameters
dest | - | pointer to the memory location to copy to |
src | - | pointer to the memory location to copy from |
count | - | number of bytes to copy |
[edit] Return value
dest
[edit] Example
Run this code
#include <stdio.h> #include <string.h> #define LENGTH_STRING 20 int main(void) { char source[LENGTH_STRING] = "Hello, world!"; char target[LENGTH_STRING] = ""; int integer[LENGTH_STRING / sizeof(int)] = {0}; printf("source: %s\n", source); printf("target: %s\n", target); printf("integer: "); for (unsigned i = 0; i < sizeof(integer) / sizeof(integer[0]); ++i) { printf("%x ", integer[i]); } printf("\n========\n"); memcpy(target, source, sizeof source); memcpy(integer, source, sizeof source); printf("source: %s\ntarget: %s\n", source, target); printf("source(hex): "); for (unsigned i = 0; i < sizeof(source) / sizeof(source[0]); ++i) { printf("%02x ", source[i]); } printf("\n"); printf("integer(hex: %s-endian): ", integer[0] == 0x48656c6c ? "big" : "little"); for (unsigned i = 0; i < sizeof(integer) / sizeof(integer[0]); ++i) { printf("%08x ", integer[i]); } printf("\n"); }
Possible output:
source: Hello, world! target: integer: 0 0 0 0 0 ======== source: Hello, world! target: Hello, world! source(hex): 48 65 6c 6c 6f 2c 20 77 6f 72 6c 64 21 00 00 00 00 00 00 00 integer(hex: big-endian): 48656c6c 6f2c2077 6f726c64 21000000 00000000
[edit] See also
moves one buffer to another (function) |
|
C++ documentation for memcpy
|