Re: [R] Passing character strings from C code to R

From: mkondrin <mkondrin_at_hppi.troitsk.ru>
Date: Sat 16 Jul 2005 - 01:32:03 EST

Tuszynski, Jaroslaw W. wrote:

>Hi,
>
>I have a C code which produces array of integers and potentially a string,
>and I have problems passing the string out. Here is the relevant part of the
>code:
>
> 1 PROTECT(Ret = allocVector(INTSXP, n));
> 2 ret = (int*) INTEGER(Ret); /* get pointer to R's Ret */
> 3 for(i=0; i<n; i++) ret[i] = data[i];
> 4 Free(data);
> 5 i=1;
> 6 if (comment) { // comment was found
> 7 n = strlen(comment);
> 8 if(n>0) { // and it actually have some length
> 9 Rprintf(" '%s' %i\n", comment, n);
> 10 PROTECT(Str = allocString(n+1));
> 11 str = CHAR(STRING_ELT(Str, 0));
> 12 strcpy(str, comment);
> 13 Rprintf(" '%s' %i\n", str, n);
> 14 setAttrib(Ret, install("comm"), Str);
> 15 i=2;
> 16 }
> 17 Free(comment);
> 18 }
> 20 UNPROTECT(i);
>
>Print statement in line 9 gives right results, but program crashes before
>print statement in line 13.
>Any ideas to what am I doing wrong?
>
>Jarek
>=====================================\====
> Jarek Tuszynski, PhD. o / \
> Science Applications International Corporation <\__,|
> (703) 676-4192 "> \
> Jaroslaw.W.Tuszynski@saic.com ` \
>
>______________________________________________
>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
>
>
SEXP charToSexp(char * val) {
  USER_OBJECT_ ans;
  PROTECT(ans = NEW_CHARACTER(1));
  if(val)

      SET_STRING_ELT(ans, 0, COPY_TO_USER_STRING(val));   UNPROTECT(1);   return(ans);
}



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 Received on Sat Jul 16 01:37:40 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:33:45 EST