Dalek Dalek - 5 months ago 29
C Question

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

I am struggling to use the concept of pointer in my

code. The following example is the simplified version of what I am trying to do. I have a function
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])

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

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)
return sp

So I would like to used the
function as an input for
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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download