How to compare pointer to struct to NULL?

I have this in Cython:

cdef extern from "lib/bindings.h":
    ctypedef struct parser:
        yyscan_t yyscanner
        ...

And related C code:

typedef struct {
    yyscan_t yyscanner;
    ...
} _parser, *parser;

Now, when I try:

cdef parser p = ...
if p != NULL:
    ...

And get:

Invalid types for '!=' (parser, void *)

Why? Does Cython misinterpret parser declaration? It seems like it doesn't "understand" that parser is a pointer type.

1 answer

  • answered 2018-03-11 18:10 DavidW

    Cython doesn't read your C code at all - it just #includes the relevant files blindly and uses your Cython ctypedef to understand what's going on. Since you haven't shown pointers in your Cython code it has no way of knowing that parser is a pointer type.

    The correct declaration would be something like

    cdef extern from "lib/bindings.h":
        ctypedef struct _parser:
            yyscan_t yyscanner
            # ...
        ctypedef _parser* parser