官术网_书友最值得收藏!

Typedef and function pointers

The typedef in C/C++ code allows the programmer to give a new name or alias to any type. For example, one could typedef an int to myint. Or you can just simply typedef a struct so that you don't have to refer to the struct with the keyword struct every time. For example, consider this C struct and typedef:

struct foobar {
  int x;
  char * y;
};
typedef struct foobar foobar_t;

In Cython, this can be described by the following:

cdef struct foobar:
    int x
    char * y
ctypedef foobar foobar_t

Note we can also typedef pointer types as below:

ctypedef int * int_ptr

We can also typedef function C/C++ pointers, as follows:

typedef void (*cfptr) (int)

In Cython, this will be as follows:

ctypedef void (*cfptr)(int)

Using the function pointer is just as you would expect:

cdef cfptr myfunctionptr = &myfunc

There is some magic going on here with function pointers as it's simply not safe for raw Python code to directly call a Python function or vice versa. Cython understands this case and will wrap things up for us to make the call safely.

主站蜘蛛池模板: 治县。| 平乡县| 花莲市| 清远市| 高邮市| 芮城县| 崇阳县| 蓬安县| 西宁市| 土默特左旗| 子洲县| 无锡市| 枝江市| 封丘县| 田林县| 子长县| 祁连县| 曲靖市| 中西区| 化德县| 河北区| 靖州| 伊宁县| 南郑县| 琼海市| 孝昌县| 迁西县| 长岛县| 布尔津县| 武汉市| 锡林郭勒盟| 无极县| 金阳县| 茶陵县| 磴口县| 阿尔山市| 遂宁市| 黄浦区| 高陵县| 宝清县| 黑水县|