Re: [Rd] .C and .Call: convolve example not working

From: Thomas Lumley <>
Date: Fri, 12 Aug 2011 09:50:39 +1200

On Fri, Aug 12, 2011 at 1:06 AM, Lars Wißler <> wrote:
> Dear R users,
> I want to call C code via the .C or .Call interface. This works fine
> with integer values but using doubles the array received in C will be
> set to zeros.
> I have tried the convolve examples (Writing R extensions, chapter 5.2)
> and still the resulting array consists of zeros.
> My code (shortened for my purposes. Original did not work either):
> ------------------------------------------------------------
> convolve.r
> a=array(1, c(4))
> b=array(3, c(4))
> print(a)
> print(b)
> .C("convolve",
>    as.double(a),
>    as.double(b))
> ----------------------------------------------------------------
> convolve.c
> void convolve(double* a, double* b){
>     int i;
>     for(i=0;i<4;i++) Rprintf("a: %d", a[i]);
>     for(i=0;i<4;i++) Rprintf("b: %d", b[i]);
> }

The C code here is wrong for two reasons. Firstly, there's no declaration for Rprintf(), because you don't include the header file.

Secondly, you're using %d to print, which means you are telling the compiler you're passing ints to Rprintf(), but you are actually passing doubles.

When I fix these problems the code works for me.


Thomas Lumley
Professor of Biostatistics
University of Auckland mailing list Received on Thu 11 Aug 2011 - 21:51:48 GMT

This quarter's messages: by month, or sorted: [ by date ] [ by thread ] [ by subject ] [ by author ]

All messages

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Fri 12 Aug 2011 - 11:50:16 GMT.

Mailing list information is available at Please read the posting guide before posting to the list.

list of date sections of archive