C 语言 memcpy() 函数用于从 source 指向的内存位置复制 n 个字节到 destination。它比字符串函数更底层,可复制任意类型数据。
原型
void *memcpy(void *destination, const void *source, size_t n);参数
destination- 目标内存指针;source- 源内存指针;n- 要复制的字节数。
返回值
返回 destination 的指针。
示例
#include <stdio.h>
#include <string.h>
int main()
{
int src[] = {1, 2, 3, 4, 5};
int dst[5];
memcpy(dst, src, 5 * sizeof(int));
for (int i = 0; i < 5; i++)
printf("%d ", dst[i]);
printf("\n");
return 0;
}输出:
1 2 3 4 5注意事项
memcpy 要求 source 与 destination 不能重叠;重叠时用 memmove。它按字节复制,不会检查 \0。