C++ memcpy的简单例子
/* Testmemcpy.cpp */
#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
方法参数:
destination copy的目的地
Pointer to the destination array where the content is to be copied, type-casted to a pointer of type void*.
source copy的源文件
Pointer to the source of data to be copied, type-casted to a pointer of type const void*.
num copy多大
Number of bytes to copy.
size_t is an unsigned integral type.
Ubuntu下的编译c++:
命令行运行
g++ -o test Testmemcpy.cpp
-o用来指定生成的可执行文件的名字。
定位到test文件的存放地址 执行./test即可运行该文件。
Ubuntu下编译C:
跟c++类似
gcc -o test Test.c