2

I have python ctypes array. I would like to use it in cython def and cdef functions.

import ctypes
from my_cython_extention import *

class Test_Node(ctypes.Structure):
    _fields_ = [
        ("i", ctypes.c_int),
        ("f", ctypes.c_float),
    ]
my_array = (Test_Node * 10)()
my_cython_function(my_array)

How should I define cython function my_cython_function to access my_array data? Can I get my_array C++ data pointer?

1 Answer 1

1

I have found an answer to the question:

cdef my_cython_function_c(
    Test_Node *arr_addr, int arr_size
):
    for i in range(arr_size):
        printf("%i\n", arr_addr.i)
        inc(arr_addr)

def my_cython_function(arr):
    my_cython_function_c(<Test_Node *><long>ctypes.addressof(arr), len(arr))
Sign up to request clarification or add additional context in comments.

1 Comment

To make this platform independent, instead of long, use intptr_t defined in 'stdint.h'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.