memcpy
函数 <cstring>
void * memcpy ( void * destination, const void * source, size_t num );
拷贝内存块
将num字节的值从source所指向的位置直接复制到destination所指向的内存块。
源指针和目标指针所指向的对象的底层类型与此函数无关;结果是数据的二进制副本。
该函数不检查source中是否有任何终止的空字符——它总是精确地复制num字节。
为了避免溢出,目标和源参数所指向的数组的大小必须至少为num字节,
并且不能重叠(对于重叠的内存块,
memmove是一种更安全的方法)。
☲ 参数
destination
指向要复制内容的目标数组的指针,类型转换为void*类型的指针。
source
指向要复制的数据源的指针,类型转换为const void*类型的指针。
num
要复制的字节数。
size_t是一个无符号整型。
☉ 返回值
返回 destination 。
☣ 示例
/* memcpy example */
#include <stdio.h>
#include <string.h>
struct {
char name[40];
int age;
} person, person_copy;
int main ()
{
char myname[] = "Pierre de Fermat";
/* using memcpy to copy string: */
memcpy ( person.name, myname, strlen(myname)+1 );
person.age = 46;
/* using memcpy to copy structure: */
memcpy ( &person_copy, &person, sizeof(person) );
printf ("person_copy: %s, %d \n", person_copy.name, person_copy.age );
return 0;
}
|
输出:
person_copy: Pierre de Fermat, 46
🍄 另请参阅