Python ctypes,使用struct typedef

时间:2012-01-20 03:53:52

标签: python ctypes

说我有以下c代码:

typedef struct _test test;

struct _test {
    test*    just_a_test;
    char*    just_a_char;
};

以下实施是否有效?

class test(Structure):
    _fields_ = [
        ('just_a_test', POINTER(test)),
        ('just_a_char', c_char_p),
    ]

我对结构中的第一个指针感到困惑。

1 个答案:

答案 0 :(得分:3)

您的代码无效,因为在引用test时,该类尚未创建。

15.17.1.16. Incomplete Types下的ctypes文档中描述的确切问题。

解决方案是在创建类后设置_fields_

class test(Structure):
    pass
test._fields_ = [
    ('just_a_test', POINTER(test)),
    ('just_a_char', c_char_p),
]