I am passing (numeric) arrays to a C function, called with .C. To the best of my knowledge, I can do this in R by passing it as a vector (with as.vector) to the .C call. However, it would be useful to access the array as an array inside C, ie not have to calculate how array indices (i,j,k) map to the vector elements. What is the right way to do this? Are there convenience functions to do the index calculations (knowing the dimensions) that I could call? In the C99 standard, I have seen something about flexible arrays, I don't know if that would help here. Thanks, Tamas
Tamas, You could write convenience functions, but I have used the C99 mechanism for variable length arrays with no problems calling from R. One thing you have to keep in mind though is that (as far as I know) the dimensions must be passed before the array reference. So for example, r <- .C("foo", as.integer(ni), as.integer(nj), x = double(ni * nj), ...) with your function defined as void foo(int *ni, int *nj, double x[*ni][*nj]) { ... Then in C you can access elements of x via x[3][4], for example. Ben
Hi Ben, Thanks for your answer. I looked at the status of VLA on the GCC homepage and it appears to be "broken". [1] Do you think that the code below still works? Or are you using a different compliler? Thanks, Tamas [1] http://gcc.gnu.org/c99status.html On Sun, Nov 19, 2006 at 09:55:17AM -0500, Benjamin Tyner wrote:> Tamas, > > You could write convenience functions, but I have used the C99 mechanism > for variable length arrays with no problems calling from R. One thing > you have to keep in mind though is that (as far as I know) the > dimensions must be passed before the array reference. So for example, > > r <- .C("foo", > as.integer(ni), > as.integer(nj), > x = double(ni * nj), > ...) > > with your function defined as > > void foo(int *ni, int *nj, double x[*ni][*nj]) > { > ... > > Then in C you can access elements of x via x[3][4], for example. > > Ben > > ______________________________________________ > R-devel at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel
Tamas, I am using GCC with no problems. You can find more examples at http://gcc.gnu.org/onlinedocs/gcc-3.3.1/gcc/Variable-Length.html Ben> Hi Ben, > > Thanks for your answer. I looked at the status of VLA on the GCC > homepage and it appears to be "broken". [1] Do you think that the code > below still works? Or are you using a different compliler? > > Thanks, > > Tamas > > [1] http://gcc.gnu.org/c99status.html > > > On Sun, Nov 19, 2006 at 09:55:17AM -0500, Benjamin Tyner wrote: > >> Tamas, >> >> You could write convenience functions, but I have used the C99 mechanism >> for variable length arrays with no problems calling from R. One thing >> you have to keep in mind though is that (as far as I know) the >> dimensions must be passed before the array reference. So for example, >> >> r <- .C("foo", >> as.integer(ni), >> as.integer(nj), >> x = double(ni * nj), >> ...) >> >> with your function defined as >> >> void foo(int *ni, int *nj, double x[*ni][*nj]) >> { >> ... >> >> Then in C you can access elements of x via x[3][4], for example. >> >> Ben >> >> ______________________________________________ >> R-devel at r-project.org mailing list >> https://stat.ethz.ch/mailman/listinfo/r-devel
Hin-Tak & Tamas, For example, see what I've done in http://www.stat.purdue.edu/~btyner/postage_0.1-1.tar.gz I am by no means a C guru but it works with my compiler. The relevant lines are (in postage.c:) void postage(int *lambda, int *D, int *tau, int r[*tau][*D]) { (in postage.R:) r <- .C("postage", as.integer(lambda), as.integer(D), as.integer(tau0), r = integer(tau0 * D), PACKAGE="postage")$r