"extract" a part of an array

Hello,
I have an array or doubles (e.g., double H[9];) which is used to store a symmetric matrix

1
2
3
H[0] = 0.0; H[1] = 1.0; H[2] = 2.0;
H[3] = 1.0; H[4] = 3.0; H[5] = 4.0;
H[6] = 2.0; H[7] = 4.0; H[8] = 5.0;


H is passed (by reference) to a function (that I can not change). In certain cases I want to pass to the function only a "sub-matrix" of H e.g.,

H[0] = 0.0; H[1] = 1.0;
H[3] = 1.0; H[4] = 3.0;

Obviously I could define another array and copy the part I need into it

1
2
3
double A[4];
A[0] = H[0]; A[1] = H[1];
A[2] = H[3]; A[3] = H[4];


or alternatively I could use memcpy, however, for my application H is large and I was wondering whether there is a more intelligent way to do that. The sub-matrix I want to pass always starts from H[0] and is square.

Thanks,
Dimitar
There's only so much you can do here i think. The function you are passing the matrix to expects an array of doubles. In memory this means that each double will be 'beside' each other. If you want to pass only a subset of that array, you will have to make a new array and copy the new values into it. Alternatively you could reorder the original matrix, and then only send the function the first N*N valuse (N being the size of the sub-matrix), but i don't think that would be very easy to implement.

It shouldn't be that hard to write a function that accepts an integer and populates a sub-matrix from the original matrix, meaning you wouldn't have to copy the entries 'by hand' as in your example below
Yes, using memcpy() is fine, remembering that you'll have to call it in a loop where there are gaps...
As I see, I am not missing something obvious. So I will stick to memcpy().

Thanks for the responses.
Dimitar
Topic archived. No new replies allowed.