Dalek Dalek - 28 days ago 11
C Question

Cannot assign type 'myFuncDef *' to 'void (*)(double *, double *)'

I am struggling to use the concept of pointer in my

cython
code. The following example is the simplified version of what I am trying to do. I have a function
func
which I would like to feed in a function (a distribution function) as an input parameter. The distribution has two pointer vectors as input variables.

from cpython cimport array
import cython
import ctypes
cimport numpy as np

ctypedef void (*myFuncDef)(double *, double *)
from cython.parallel import prange
cdef void func(int* x, double* hx, void(*func)(double*, double*), int n):
def int i
for i from 0 <= i < n:
func[0](&x[i], &hx[i])
return

cpdef void Lognormal(double* u,
double* yu):
#evaluate log of normal distribution
yu=-u*u*0.5
return

def foo(np.ndarray[ndim=1, dtype=np.float64_t] x,
np.ndarray[ndim=1, dtype=np.float64_t] hx,
myFuncDef distribution, int k):
cdef np.ndarray[ndim=1, dtype=np.float64_t] sp
cdef int num=len(x)
cdef int j
for j from 0 <= j <k:
func(&x[0],&hx[0],&distribution, num)
sp[j]=hx[0]
return sp


So I would like to used the
Lognormal
function as an input for
foo
function. I get the following error message:

Cannot assign type 'myFuncDef *' to 'void (*)(double *, double *)'


I will appreciate for any suggestion to fix this bug.

Answer Source

You have one too many layers of pointer (the standard issue raised to argue against typedefs for pointers). Just drop the & from &distribution; it's already the function pointer you want.