0

Say I have the following c code:

typedef struct _test test;

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

Would the following implementation work?

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

I'm just confused regarding the first pointer in the structure.

1 Answer 1

3

Your code will not work because at the time it references test, the class hasn't been created yet.

This exact problem in described in ctypes documentation under 15.17.1.16. Incomplete Types.

The solution is to set _fields_ after creating the class:

class test(Structure):
    pass
test._fields_ = [
    ('just_a_test', POINTER(test)),
    ('just_a_char', c_char_p),
]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.