user0
user0

Reputation: 127

How to declare linked lists with pointers in Cython

I am trying to create a linked list in Cython for the following C code:

typedef struct my_struct *my_val;

typedef struct my_struct {
      int i;
      my_val next_val;
}

This didn't work:

 cdef my_struct* my_val

 cdef struct bc_struct:
      int i
      my_val next_val

I get this error on the first cdef:

'my_val' is not a type identifier

Neither did this:

cdef struct my_struct* my_val

This gives an error on the first cdef:

Syntax error in struct or union definition

Any help is much appreciated!

Upvotes: 2

Views: 826

Answers (1)

Yastanub
Yastanub

Reputation: 1237

From what i have read so far cdef is not the equivalent of a typedef. Instead you are declaring C variables. Try using ctypedef instead as descriped in the link.

EDIT: A bit more explanation. When you use cdef mystruct* my_val you are not performing a typedef, instead you are declaring the C variable my_val which is of type mystruct *. When you now try to use it as a type specifier like in your class, it of course tells you that it is not a specifier since it is a variable. If you use ctypedef instead it performs a typedef

ctypedef mystruct *my_val

for the second cdef struct mystruct* my_val it throws a syntax error because the syntax is cdef type name and thus you try to initialize a variable called mystruct* of type struct and have also a trailing my_val expression behind it.

Upvotes: 2

Related Questions