I'd like to create my very own list container using Cython. I'm a very new begginer to it, and following the documentation I could get to creating such a structure :
cdef struct s_intList:
int value
void* next
ctypedef s_intList intList
but when comes the time to acces the struct members, I can't find the good syntax:
cpdef void foo():
cdef intList* li
# li.value OR li->value
throws : "warning: intlists.pyx:8:12: local variable 'li' referenced before assignment" which let me assume that my cython structs usage is incorrect...
Any idea of what I'm doing wrong here please? :) Thank you for you help
In your code, li
is a pointer to an intList
. This pointer is not initialized to point to anything, so accessing li.value
is meaningless (and erroneous).
In fabrizioM's answer, an intList
is created (not a pointer to one) on the stack, so there is a location in memory reserved for li.value
.
If you want to create an intList
with actual data (which I gather you intend to be like a linked list data structure), and if you want to be able to return that intList
from functions, etc. you will have to allocate your intList
structs on the heap and build up the full linked list from there. Cython allows you to call malloc
(and free
) easily to do this.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With