Take the 2-minute tour ×

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;


k1 = (double[][])params;


k1 = *(double *)params;


I guess the question is: is there a one line solution?

share|improve this question
add comment

1 Answer

up vote 5 down vote accepted

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


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

share|improve this answer
thanks! I'm aware I can simply pass k1 (and not &k1), but I'm trying to stay consistent within the code. Not sure how necessary that is for the time being... –  Rasman May 12 '11 at 13:05
interesting note: i don't need a dummy variable after all... it seems to work with double (*k1)[2][4] = params; –  Rasman May 12 '11 at 13:18
add comment

Your Answer


