gpt4 book ai didi

c - 结构中的数组

转载 作者:太空宇宙 更新时间:2023-11-04 00:55:16 24 4
gpt4 key购买 nike

我是 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);
}

当我尝试编译这个时,我得到错误:从类型“double (*)[151]”分配给类型“double[151][151]”时类型不兼容

我目前的解决方法是在代码中将“J[x][y]”替换为“data->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);
}

关于c - 结构中的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5071410/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com