开发者

passing double array by void type for GSL in C

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 suppos开发者_JAVA技巧ed 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?


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) where n is the number of elements in each row).

I.e.

 double x[5][2];

 int main()
 {
     double (*y)[5][2];

     void *z = x;

     y = z;


     /* after you extract the pointer from 'z' you 
        can access the elements using (*y)[][] */
     (*y)[1][1] = 1.0;
 }

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).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜