-2

Say we have two 2d arrays:

double matrix [64][100];
double array[64][32];

And we want to copy 32 elements from matrix[64][50:82] to array[64][32] using memcpy. Do you the solution?

1 Answers1

1
double matrix[64][100];
double array[64][32];

for (int i = 0; i < 64; ++i) {
    memcpy(&array[i][0], &matrix[i][50], sizeof(double) * 32);
}

However, consider using std::copy() or std::copy_n() instead. They will use memcpy() internally when safe to do so:

#include <algorithm>

double matrix[64][100];
double array[64][32];

for (int i = 0; i < 64; ++i) {
    std::copy(&matrix[i][50], &matrix[i][82], &array[i][0]);
    or
    std::copy_n(&matrix[i][50], 32, &array[i][0]);
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770