在数组钢结构 [英] Arrays in stuctures

查看:214
本文介绍了在数组钢结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新的C和嵌入结构时具有数组类型的问题。以下是我的问题的例子:

  typedef结构{
    双J [151] [151];
} *用户数据;静态INT PSOLVE(无效* USER_DATA,N_Vector溶液)
{
数据的UserData;
数据=(的UserData)USER_DATA;双J [151] [151];
J = DATA->焦耳;/ *解决使用J A矩阵方程,储存在解决方案* /返回(0);
}

当我尝试编译此我得到
错误:不兼容的类型分配给输入时,双[151] [151]从类型'双(*)[151]

我给这家目前的解决方法已取代'J [X] [Y]用数据 - >Ĵ[X] [Y]在code,解决了矩阵方程,但分析表明这是低效率的。

更改参数PSOLVE是不是一种选择,因为我使用的日晷,CVODE求解其中prescribes的参数的类型和顺序。

感谢您的帮助,
安德鲁


解决方案

  typedef结构{
    双J [151] [151];
} 用户数据; //这是一个新的数据结构,而不应指针!静态INT PSOLVE(无效* USER_DATA,N_Vector溶液)
{
*的UserData数据; //这应该是一个指针,而不是!
数据=(*的UserData)USER_DATA;双J [151] [151];
的memcpy(J线,数据>Ĵ,sizeof的(双)* 151 * 151); //使用的memcpy()将内容从一个阵列复制到另一个/ *解决使用J A矩阵方程,储存在解决方案* /返回(0);
}

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.

Thank you for the help, Andrew

解决方案

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

这篇关于在数组钢结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆