Sev*_*ess 1 c arrays structure
我是C的新手,在嵌入结构时遇到阵列类型问题.以下是我的问题的一个例子:
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);
}
Run Code Online (Sandbox Code Playgroud)
当我尝试编译这个时,我得到错误:从类型'double(*)[151]'分配类型'double [151] [151]'时出现不兼容的类型
我目前的解决方法是用代码中的'data-> J [x] [y]'替换'J [x] [y]'来求解矩阵方程,但是分析表明这样效率较低.
将参数更改为PSolve不是一个选项,因为我正在使用sundials-cvode解算器来规定参数的类型和顺序.
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);
}
Run Code Online (Sandbox Code Playgroud)