In the documentation for the .C function in R here, it says that the C function itself must be of return type void. I’m working on some inherited code that uses the .C function (rather than .Call or Rcpp), but I’d like to write a function that “returns” a vector of unknown size. If I knew the size of this vector, I could allocate it in R and pass the pointer to C, whose contents can then be modified. Given that I don’t know its size, what is the way to declare it in R? I imagine it will have this shape, but I don’t know the details:
output <- ?????
storage.mode(a) <- "integer"
tmp <- .C("myfunc",a,output)
I suggest you do use
.Call()instead of.C()— see recent discussions on the r-devel list. I conjecture that you will find close to no experienced R developers suggesting to use.C()for new projects.Here is a simple illustration. You are by no means forced to use Rcpp, the same code can be written in the plain C API as well, albeit not as compactly:
In the old
.C()interface, you can only return atomistic C types like*intor*doubleand you cannot allocate space at the C level as you can only “mark”SEXPobject for management by R when you can actually returnSEXPobjects — and only.Call()can.