Skip to content

problem when I Call C subfunction in void function

4 messages · eguichard, John McKown, Thomas Lumley

#
Hi everybody,
I am including some C code in R program using the .C interface.
I would like use a C subfunction calling by C void function.

/double essai (double Px[], int tailleP)
{
	Rprintf ("I print Px %d\t", Px[1]);
	return 57;
}

void test_essai (double *Px, int *tailleP, double *res)
{
	*res = essai(*Px, *tailleP);
}/

This program doesn?t work. When I compile it, I have the following error : 
?In function ?test_essai :
-	Error: incompatible type for argument 1 of ?essai?
-	Note : expected ?double*? but argument is of type ?double? ?

Does anyone have solution ? 
Thank all, Elie 




--
View this message in context: http://r.789695.n4.nabble.com/problem-when-I-Call-C-subfunction-in-void-function-tp4696073.html
Sent from the R help mailing list archive at Nabble.com.
#
On Wed, Aug 27, 2014 at 4:58 AM, eguichard <elie.guichard at inserm.fr> wrote:
This line is wrong. Try:
           *res = essi(Px, *tailleP);

Note that double *Px and double Px[] are basically different ways to
say pretty much the same thing. At least in C. Not in C++ or many
other languages. But further discussion would be even more off topic.

  
    
#
Thank you. 
I can improve my program with your response but I have an other problem. 

/double essai (double *Px, int *tailleP)
{
	int i;
	for (i = 0; i < *tailleP; i++)
	{
		Px[i]=Px[i]*2;
		Rprintf ("I print Px %f\t", Px[i]);
	}
	Rprintf("\n");
	return *Px; 
}

void test_essai (double *Px, int *tailleP, double *res)
{
	int i;
	*res = essai(Px, tailleP); 
	for (i = 0; i < *tailleP; i++)
	{
		Rprintf ("I print res %f\t", res[i]);
	}
}/

When I compile it in R 
/trajPx <- c(2,5,5,4)
.C("test_essai",Px = as.numeric(trajPx),tailleP =
as.integer(length(trajPx)),res = numeric(4))/

I have the following result: 

I print Px 4.000000     I print Px 10.000000    I print Px 10.000000    I
print Px 8.000000     
I print res 4.000000    I print res 0.000000    I print res 0.000000    I
print res 0.000000   
 $Px
[1]  4 10 10  8

$tailleP
[1] 4

$res
[1] 4 0 0 0
 
I haven't problem in "essai" function but I can't correctly return "Px"
vector.
I d'ont understand why I get only the first number (number 4 in my exemple)
?




--
View this message in context: http://r.789695.n4.nabble.com/problem-when-I-Call-C-subfunction-in-void-function-tp4696073p4696103.html
Sent from the R help mailing list archive at Nabble.com.
#
On Thu, Aug 28, 2014 at 3:36 AM, eguichard <elie.guichard at inserm.fr> wrote:
*Px is a single double, the same as Px[0]
This is more clearly written as
          res[0] = essai(Px, tailleP);

It sets the first element of res to the double returned by essai()

If you want essai() to modify all the elements of the array that res
points to, you need to pass res to the function.