[R] Trouble passing arrays to C code

Antonio, Fabio Di Narzo antonio.fabio at gmail.com
Sat Dec 2 16:17:21 CET 2006


2006/12/2, Wee-Jin Goh <wjgoh at brookes.ac.uk>:
> 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,

here 'steps' is double, was integer in 'lorenz_run'.

>                         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
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>


--
Antonio, Fabio Di Narzo
Ph.D. student at
Department of Statistical Sciences
University of Bologna, Italy




More information about the R-help mailing list