我刚刚开始使用python,我想知道我应该如何用python定义联合(使用ctypes)?希望我是对的,通过 ctypes 支持工会。例如下面的 c 代码在 python 中是如何的
struct test
{
char something[10];
int status;
};
struct test2
{
char else[10];
int status;
int alive;
};
union tests
{
struct test a;
struct test2 b;
};
struct tester
{
char more_chars[20];
int magic;
union tests f;
};
Thx,如果其他人正在寻找相同的答案,则添加简单示例
from ctypes import *
class POINT(Structure):
_fields_ = [("x", c_int),
("y", c_int)]
class POINT_1(Structure):
_fields_ = [("x", c_int),
("y", c_int),
("z",c_int)]
class POINT_UNION(Union):
_fields_ = [("a", POINT),
("b", POINT_1)]
class TEST(Structure):
_fields_ = [("magic", c_int),
("my_union", POINT_UNION)]
testing = TEST()
testing.magic = 10;
testing.my_union.b.x=100
testing.my_union.b.y=200
testing.my_union.b.z=300