I'm new to C and having a problem with array types when embedded in structures. The following is an example of my issue:
typedef struct {
double J[151][151];
} *UserData;
static int PSolve(void *user_data, N_Vector solution)
{
UserData data;
data = (UserData) user_data;
double J[151][151];
J = data->J;
/* Solve a matrix equation that uses J, stored in 'solution' */
return(0);
}
When I try to compile this I get error: incompatible types when assigning to type ‘double[151][151]’ from type ‘double (*)[151]’
My current workaround for this has been to replace 'J[x][y]' by 'data->J[x][y]' in the code to solve the matrix equation, but profiling has shown this to be less efficient.
Changing the arguments to PSolve is not an option because I'm using the sundials-cvode solver which prescribes the type and order of the arguments.
typedef struct {
double J[151][151];
} UserData; // This is a new data structure and should not a pointer!
static int PSolve(void *user_data, N_Vector solution)
{
UserData* data; // This should be a pointer instead!
data = (UserData*) user_data;
double J[151][151];
memcpy(J, data->J, sizeof(double) * 151 * 151); // use memcpy() to copy the contents from one array to another
/* Solve a matrix equation that uses J, stored in 'solution' */
return(0);
}