C++ 从偏移位置开始复制内存

C++ 从偏移位置开始复制内存,c++,offset,memcpy,C++,Offset,Memcpy,如何从给定偏移量开始复制内存。 比如说 int main() { int a1[100], a2[100], i; errno_t err; // Populate a2 with squares of integers for (i = 0; i < 100; i++) { a2[i] = i*i; } // Tell memcpy_s to copy 10 ints (40 bytes), giving // the si

如何从给定偏移量开始复制内存。 比如说

int main()
{
   int a1[100], a2[100], i;
   errno_t err;

   // Populate a2 with squares of integers
   for (i = 0; i < 100; i++)
   {
      a2[i] = i*i;
   }

   // Tell memcpy_s to copy 10 ints (40 bytes), giving
   // the size of the a1 array (also 40 bytes).
   err = memcpy_s(a1, sizeof(a1), a2, 10 * sizeof (int) );    
   if (err)
   {
      printf("Error executing memcpy_s.\n");
   }
   else
   {
     for (i = 0; i < 10; i++)
       printf("%d ", a1[i]);
   }
   printf("\n");
}
intmain()
{
int a1[100],a2[100],i;
错误没有错误;
//用整数的平方填充a2
对于(i=0;i<100;i++)
{
a2[i]=i*i;
}
//告诉memcpy_s复制10个整数(40字节),给出
//a1数组的大小(也是40字节)。
err=memcpy_s(a1,sizeof(a1),a2,10*sizeof(int));
如果(错误)
{
printf(“执行memcpy_s时出错。\n”);
}
其他的
{
对于(i=0;i<10;i++)
printf(“%d”,a1[i]);
}
printf(“\n”);
}
如何从a1的索引50开始将内存从a2复制到a1


提前感谢

将50添加到
a1
。添加时无需弄乱sizeof;编译器知道如何操作。

将地址传递给要复制到的索引,作为
memcpy的目标:

memcpy(&a1[50], &a2[50], 10 * sizeof a[0]);

或者相当于,
a1+50,a2+50
@delnan,是的。然而,我个人更喜欢我的写作方式。你能给我看一段代码片段吗。如果char*data是目标变量。
err=memcpy_s(a1+50,50*sizeof(*a1),a2,10*sizeof(int))
我没有注意到原始代码使用的是
memcpy_s
;为此,还必须调整目标缓冲区的大小。