Skip to content
Prev 40407 / 63424 Next

C-Side: Applying a function (given as param) to data (given as param)

On Fri, Jun 03, 2011 at 11:14:39AM -0500, Douglas Bates wrote:
I already read inside it.
If there would be no question open i would not have registered to this mailing list
and woulod not have asked that question.
Obviously my question was not answered in the "Writing R Extensions",
so if you can give me a hint this would be nice.
I use C, not C++.

But maybe it helps too.

I already created my own package with R and C successfully some days ago.

But so far I did not used fucntion pointers coming in via SEXP parameters.

And that was my specific question.
As just mentioned: I already created successfully a C-extension for R.

But I would like to know, how to call a function that I get via
SEXP as parameter. How can I find out the function definition,
for example the arity of the function and which arguments
a function uses.

The problem is, that the C-standard (at least the first ANSI-C standard)
does not guarantee portability for C-pointers.
To be portable, for example the function pointer you use must
definitely be of same type as the function you use.

So I need to know how I can use the SEXP-function pointers.


To be more concrete, this is how my function's API looks like
at the moment:

SEXP movapply( SEXP data, SEXP width, SEXP func )
{
  /* some code */

  return ( result );
}



  data   will be vector or list or matrix
  width  will be int value (I think vector of length 1)
  func   will be a function pointer of the type that is given as argument

I don't know on which kind of pointer to cast.
I think I will cast to a pure C-type, but then all data also must
match to the function definition.

What if the function wants to work on integer, but the data is double?

Somehow this must be handled, and I think the experts here can just point me
directly to some kind of docs or maybe older postinmgs here, which explain this?


Ciao,
   Oliver