stois21
stois21

Reputation: 59

Cython error: Cannot convert Python object to type 'int *' error, when passing an Integer array to a function

I am trying to pass an integer array to a function in Cython and I cannot understand why I get the error mentioned in the title.

A sample code of what I am trying to do is the following:

cpdef foo(int *table):
for i in range(10):
    table[i] = i

cdef int *temp=<int *>malloc(10*sizeof(int))
foo(temp)

for i in range(10):
    print temp[i]

I would appreciate any pointers as to how to pass the array correctly to the function. Thank you.

Upvotes: 2

Views: 2595

Answers (1)

Gareth Latty
Gareth Latty

Reputation: 88977

The issue here is the function is cpdef - this means it can be called from both C and Python code, and means all arguments must be Python objects (otherwise how would one call it from Python?)

Declare it as cdef instead.

Upvotes: 7

Related Questions