Can I create a static C array with Cython?

2020-05-20 07:51发布

问题:

I'd like to do exactly this in Cython:

cdef int shiftIndexes[] = [1,-1, 0, 2,-1, -1, 4, 0, -1, 8, 1, -1, 16, 1, 0, 32, 1, 1, 64, 0, 1, 128, -1, 1]

I've seen a few references in fixed bug reports and old email lists that static array functionality exists in Cython, but I can't find anty examples and this particular example gives me a syntax error: Syntax error in C variable declaration

Is it possible to make static C arrays with Cython?

回答1:

Use pointer notation instead:

cdef int *shiftIndexes = [1,-1, 0, 2,-1, -1, 4, 0, -1, 8, 1, -1, 16, 1, 0, 32, 1, 1, 64, 0, 1, 128, -1, 1]

And it will work like a charm.