[R] converting R objects to C types in .Call

Prof Brian Ripley ripley at stats.ox.ac.uk
Mon Jan 24 19:28:47 CET 2005


On Mon, 24 Jan 2005, Faheem Mitha wrote:

> I'm trying to write an R wrapper for a C++ library, using .Call. I've never 
> used .Call before. I'm currently having some difficulties converting a R 
> character string to a C one.

> Here is a little test program.
>
> #include <R.h>
> #include <Rinternals.h>
> #include <stdio.h>
>
> SEXP testfn(SEXP chstr)
> {
>  char * charptr = CHAR(chstr);

CHAR(STRING_ELT(chstr, 0));

First you select the first element of the character vector, then select 
its contents as a C-level character array.

>  printf("%s", charptr);
> }
>
> This compiles without problems, but when I try to run this as
>
>> .Call("testfn", "foo")
>
> I get a segmentation fault.
>
> I am sure I am making an obvious mistake, but can someone help me to sort it 
> out? Thanks in advance. Please cc me, I'm not subscribed.

Please read `Writing R Extensions' for more examples.

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595




More information about the R-help mailing list