Reputation: 175
I have a pointer to a double matrix dmatrix which I declared using:
double* dmatrix = malloc(n*n*sizeof(double));
I also have a void pointer to a matrix imatrix, which has int values for a matrix of the same dimensions. The int matrix is filled elsewhere in the program. How can I copy the values from imatric to dmatrix using just pointers? This is what I an trying.:
void *a;
for(int c=0;c<n;c++){
for(int r=0;r<n;r++){
a = ((char *)imatrix+(r*n+c)*sizeof(int));
*(dmatrix+r*n+c) = *(double *)a;
}
}
Upvotes: 2
Views: 584
Reputation: 723
Wrong. 1. You say imatrix points to int matrix, while you convert it to char*. Why? 2. a is supposedly a pointer in the source matrix. Why do you cast it to (double *)?
I would do something like that:
void* imatrix;
double* dmatrix;
double* p_dmatrix = dmatrix;
int* p_imatrix = imatrix;
for(int i=0; i< r*c; i++)
*p_dmatrix++ = *p_imatrix++;
Upvotes: 4