I have a matrix stored in a float array, and I need to swap a row of one matrix into another.
This is what I have done so far. The matrices m and m_new are a matrix class, where, ‘float* m = new float(N*N)’ is the class variable ‘m’. I made a pointer to the row of the matrix where it needs to be swapped, and I used memcpy for the size of a row, ‘N’, to place that section of ‘m’ into ‘m_new’. However, the values of m_new do not change, and I am confused why.
for (int variable : partition) {
float* m_pt = m->m + (variable * N * sizeof(float));
float* m_new_pt = m_new->m + (row * N * sizeof(float));
memcpy(&m_new_pt, &m_pt, N*(sizeof(float)));
}
>Solution :
To fix this issue, you should remove the & operator in the memcpy function.
for (int variable : partition) {
float* m_pt = m->m + (variable * N);
float* m_new_pt = m_new->m + (row * N);
memcpy(m_new_pt, m_pt, N * sizeof(float));
}