Re: [R] .C interface and Strings...

From: Sundar Dorai-Raj <sundar.dorai-raj_at_pdf.com>
Date: Fri 12 Jan 2007 - 12:32:27 GMT

Stephane Cruveiller said the following on 1/12/2007 4:15 AM:
> Dear R users,
>
> I am trying to include C code into R via the .C interface. I have read
> that arguments passed to a C function have to be correctly DEreferenced.
> This is something that can be easily done for numbers (integers or
> float) by adding
> a * before the reference like for instance:
>
> ------------------------------------------------------------------------
> #include<R.h>
>
>
> void hellofct(int *n)
> {
> int i;
> for (i=0;i<*n;i++)
> {
> Rprintf("Hello, world!\n");
> }
> }
> ------------------------------------------------------------------------
> However, I can not figure out how that can be achieved for strings.
> My prototype function would be something like:
>
> ------------------------------------------------------------------------
> #include<R.h>
>
>
> void displaystring(char *str)
> {
> Rprintf("String displayed:%s\n", ????);
> }
> ------------------------------------------------------------------------
> any hints?
>

Yes, Section 5.2 in "Writing R Extensions".

<untested>

displaystring.c:
#include<R.h>
void displaystring(char **str, int *n)
{

   int i;
   for(i = 0; i < *n; i++)
     Rprintf("String displayed:%s\n", str[i]); }

displaystring.dll:
R CMD SHLIB displaystring.c

displaystring.R:
dyn.load("displaystring")
displaystring <- function(x) {

   .C("displaystring", x, length(x))
   invisible()
}
displaystring(c("A", "B"))

</untested>

HTH, --sundar



R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Fri Jan 12 23:40:38 2007

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Fri 12 Jan 2007 - 14:00:27 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.