[R] Trouble passing arrays to C code
Wee-Jin Goh
wjgoh at brookes.ac.uk
Sat Dec 2 14:44:58 CET 2006
Hello,
I'm having more trouble with interfacing with C code. I have a
function in C that will return the result of its computation as 3
arrays. The signature of the function is as follows:
void lorenz_run(double x0, double y0, double z0, double h, int steps,
double *res_x, double *res_y, double *res_z)
The function works, as I've tested it from within C itself and the
results it gives are accurate. In order to integrate it with R, I've
written the following C wrapper function:
void lorenz_run_R_wrapper(double *x0, double *y0, double *z0, double
*h, double *steps,
double *res_x, double *res_y, double *res_z) {
lorenz_run(*x0, *y0, *z0, *h, *steps, res_x, res_y, res_z);
}
The corresponding R wrapper function is:
lorenz_run <- function(x0,y0,z0,h,steps) {
returned_data = .C("lorenz_run_R_wrapper",
x0=as.double(x0),
y0=as.double(y0),
z0=as.double(z0),
h=as.double(h),
steps=as.integer(steps),
res_x=double(steps),
res_y=double(steps),
res_z=double(steps))
# Return the value of the result parameter
return( rbind(returned_data$res_x, returned_data$res_y,
returned_data$res_z))
}
My problem is, that the first elements of res_x, res_y, and res_z are
correctly set. But the rest of the cells in those 3 arrays remain 0.
This leads me to think that I am passing these arrays to the C code
wrongly. Can anyone help me out?
Thanks in advance,
Wee-Jin
More information about the R-help
mailing list