Hi Barry,

Thanks for your reply.

On Mon, 24 Jan 2005, Barry Rowlingson wrote:

Firstly, R is expecting an SEXP as a return value!

Ouch...

I haven't found anything that says this explicitly, but it looks like .Call expects a SEXP to be returned to R. At any rate, trying to use void instead gives a segfault.

And secondly, your SEXP chstr is still a vector - so you need to get an element from it. If there's only one element, then that'll be the zeroth element. Here's my code:

#include <R.h>
#include <Rinternals.h>
#include <stdio.h>

SEXP testfn(SEXP chstr)
{
 char * charptr = CHAR(VECTOR_ELT(chstr,0));
 printf("%s", charptr);
 return(chstr);
}

- I'm just returning the same object back in the return() statement, and using VECTOR_ELT(chstr,0) to get to the 0'th element.

SO in R:

> foo = .Call("testfn","fnord")

should print 'fnord' and return foo as "fnord".

> foo = .Call("testfn",c("bar","baz"))

will print 'bar' and return foo as c("bar","baz")

Thanks for your help. That works fine. Faheem.

______________________________________________
[email protected] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html

Reply via email to