[Rd] allocating character string of given size for .C

Prof Brian Ripley ripley at stats.ox.ac.uk
Wed Aug 2 17:21:05 CEST 2006


On Wed, 2 Aug 2006, Tamas K Papp wrote:

> Hi,
> 
> I have a C function (called via .C) which returns (by writing into
> buffers) some values of known (or fixed) length, all of these are
> numerical so I can allocate storage for them via as.double() and
> as.integer().
> 
> However, there is a potential error message that I would like to
> return into a char * buffer [1].  How can I allocate it in R (eg
> something similar to as.integer(), but taking the number of
> characters).

It's a long time since I have done that, but AFAIR in S you needed to pass 
a length-one character vector containing a string of the appropriate 
maximal length.  In R it is a little simpler: you are given a char** 
pointer p, and when returning from C, .C() copies the valued pointed to by 
p[i] to the ith element of a character vector via mkChar.  So you need to 
pass a length-one character vector with anything in the first element, 
e.g. character(1).

> I would like to avoid .Call if possible, this is the only tricky
> argument.
> 
> Thanks,
> 
> Tamas
> 
> [1] I would of course use strncpy to avoid overflows.

-- 
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-devel mailing list