Search code examples
c++offsetmemcpy

copy memory starting from an offset position


how do i copy memory starting from a given offset. For example

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");
}

How do I copy memory from a2 to a1 starting at index 50 of a1.

thanks in advance


Solution

  • Add 50 to a1. No need to mess with sizeof for the addition; the compiler knows how to do it.