From jahftw at googlemail.com  Fri Aug 12 10:47:48 2011
From: jahftw at googlemail.com (=?ISO-8859-1?Q?Lars_Wi=DFler?=)
Date: Fri, 12 Aug 2011 10:47:48 +0200
Subject: [Rd] .C and .Call: convolve example not working
In-Reply-To: <CAJ55+d+vxv6nskRwqo1xp30EkPx4sVCYvAMGZpULqJUx7QpAjA@mail.gmail.com>
References: <CADJn3hfrTG9dOMi3QKqZ0FmgOTtU96D+QsxU1c1xK1CA-3PMrQ@mail.gmail.com>
	<CAJ55+d+vxv6nskRwqo1xp30EkPx4sVCYvAMGZpULqJUx7QpAjA@mail.gmail.com>
Message-ID: <CADJn3hcKpNc5z+20PeBDicEjKbx3jky9QFR_UfknSKQGBgAtuw@mail.gmail.com>

Thanks a lot for the help Thomas and William, that solved it. I am
used to programming Java and didn't think of checking my print
function.

Regards Lars

2011/8/11 Thomas Lumley <tlumley at uw.edu>:
> On Fri, Aug 12, 2011 at 1:06 AM, Lars Wi?ler <jahftw at googlemail.com> 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
>
> Thomas Lumley
> Professor of Biostatistics
> University of Auckland
>



[Rd] .C and .Call: convolve example not working
=?ISO-8859-1?Q?Lars_Wi=DFler?=