Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何为Python3中ctypes定义的零长度数组赋值_Python_Arrays_Ctypes_Zero - Fatal编程技术网

如何为Python3中ctypes定义的零长度数组赋值

如何为Python3中ctypes定义的零长度数组赋值,python,arrays,ctypes,zero,Python,Arrays,Ctypes,Zero,这里我定义了一个类,如下所示,它的属性数据是一个零长度数组。如何实例化这个类 类Framectypes。结构: _字段=[ id,ctypes.c_uint64, 尺寸,ctypes.c_uint32, 数据,ctypes.c_字节*0零长度数组 ] 我试过这个 帧=帧 frame.id=0 frame.size=2 frame.data=ctypes.castb'12',ctypes.POINTERctypes.c_type*0 但第4行提出了一个例外 TypeError: incompati

这里我定义了一个类,如下所示,它的属性数据是一个零长度数组。如何实例化这个类

类Framectypes。结构: _字段=[ id,ctypes.c_uint64, 尺寸,ctypes.c_uint32, 数据,ctypes.c_字节*0零长度数组 ] 我试过这个

帧=帧 frame.id=0 frame.size=2 frame.data=ctypes.castb'12',ctypes.POINTERctypes.c_type*0 但第4行提出了一个例外

TypeError: incompatible types, LP_c_byte_Array_0 instance instead of c_byte_Array_0 instance
那么,我应该如何正确地实例化这个类呢

在C语言中,为了使变量数据紧跟在结构之后,通常使用这种技术,将长度为0的数组作为结构的最后一个成员。不幸的是,Python层不允许为数组分配大于其长度的值。最接近您的目标是将数据成员声明为指针。进一步,创建一个执行所有转换的setter方法:

代码00.py:

!/usr/bin/env蟒蛇3 导入系统 将ctypes导入为ct 类Framect.Structure: _字段=[ id,ct.c_uint64, 尺寸,ct.c_uint32, 数据,ct.POINTERct.c_ubyte,将其设置为指针 ] def set_数据自身,值: 如果不是isinstancevalue,字节数: 应为raise VALUERRORBETES。 self.data=ct.castvalue,ct.POINTERct.c_ubyte self.size=lenvalue def主: 帧=帧 frame.set\u数据集 对于rangeframe.size中的i: 打印{0:d}-{1:d}.formati,frame.data[i] 如果uuuu name uuuuu==\uuuuuuuu main\uuuuuuuu: sys.version.split中项目的{2:s}\n.format.joinitem.strip上的printPython{0:s}{1:d}位\n,如果sys.maxsize>0x100000000,则为64,否则为32,sys.platform 主要的 打印\n一张。 输出:

[cfati@CFATI-5510-0:e:\Work\Dev\StackOverflow\q059172596]>e:\Work\Dev\VEnvs\py\u 064\u 03.07.03\u test0\Scripts\python.exe code00.py Python 3.7.3 v3.7.3:ef4ec6ed12,2019年3月25日,22:22:05[MSC v.1916 64位AMD64]win32上的64位 0 - 49 1 - 50 2 - 51 3 - 97 4 - 98 5 - 67 6 - 68 完成。
谢谢我想使用memoryviewframe_instance.tobytes序列化帧对象。如果字段数据是ctypes.POINTER,则序列化结果不正确。然后,应向Frame类添加另一个方法,该方法返回一个大小为的数组,其中包含指针的内容。但这将是另一个问题的主题: