**C++ memcpy的简单例子**

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

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值