在 C 中通过 void 类型为 GSL 传递双精度数组
我正在尝试使用 GSL 库来解决 ODE,但使用 void 指针时遇到一些困难
我需要在应该包含数组的数组上发送一个参数:
double k1[2][4];
发送到此
gsl_odeiv_system sys = {func, jac, 2, &k1};
的参数会传递给两者func 和 jac 作为
int func (double t, const double y[], double f[], void *params)
func 中的 *params
,我试图通过以下方式提取 k1:
double k1[2][4];
k1 = *(double[][])params;
或
k1 = (double[][])params;
或...
k1 = *(double *)params;
等等
我想问题是: 有没有单行解决方案?
I'm trying to use the GSL library to solve ODE and I'm having some difficulty using the void pointer
I need to send a parameter over that supposed to contain an array of an array:
double k1[2][4];
which gets sent to
gsl_odeiv_system sys = {func, jac, 2, &k1};
this gets passed on to both func and jac as *params
int func (double t, const double y[], double f[], void *params)
in func, I'm trying to extract k1 via:
double k1[2][4];
k1 = *(double[][])params;
or
k1 = (double[][])params;
or...
k1 = *(double *)params;
etc
I guess the question is: is there a one line solution?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不认为你可以像这样转换为数组类型(多维数组)。您可能需要声明一个临时变量来保存指向数组第一个元素的指针。
当然,您需要指定每行的元素数量才能正常工作。否则,编译器不知道如何访问结果数组中的元素(请记住,
x[i][j]
在内部转换为*(x + i*n + j)
code> 其中n
是每行中的元素数量)。即顺便说
一句,将数组传递给函数时不必使用
&k1
。数组的名称可以用作其地址(指向第一个元素的指针)。I don't think you can cast to an array type (a multidimensional array) like this. You may need to declare a temporary variable to hold the pointer to the first element of the array.
Of course, you need to specify the number of elements per row for this to work. Otherwise the compiler does not know how to access elements in the resulting array (remember that
x[i][j]
is converted internally to*(x + i*n + j)
wheren
is the number of elements in each row).I.e.
By the way, you don't have to use
&k1
when passing the array to the function. The name of the array may be used as its address (A pointer to the first element).