python函数指针数组 指针python数据结构

python有没有指针如果您曾经使用过C或C ++等低级语言 , 那么您可能已经听说过指针 。指针允许您在部分代码中创建高效率 。它们也会给初学者带来困惑,并且可能导致各种内存管理错误,即使对于专家也是如此 。那么在Python中有指针的存在吗?
指针广泛用于C和C ++ 。本质上,它们是保存另一个变量的内存地址的变量 。有关指针的更新,可以考虑在C指针上查看此概述 。
为什么Python没有指针?
实际上指针为何不存在的原因现在还不知道 , 也许指针违背了Python的禅宗 。指针鼓励隐含的变化而不是明确的变化 。但通常情况下,它们很复杂而不是很简单,特别是对于初学者 。更糟糕的是 , 当他们用指针指向自己的方法,或做一些非常危险的事情,比如从你无法获取的的一些变量中读取数据 。
Python更倾向于尝试从用户那里抽象出内存地址来实现具体细节,所以Python通常关注可用性而不是速度 。因此,Python中的指针并没有多大意义 。但是在有些情况下,Python会为您提供使用指针的一些好处 。
想要理解Python中的指针,需要理解Python实现指针功能的具体细节 。简单来说,需要了解这些知识点:
不可变对象和可变对象【Python中的对象】
Python变量/名称【Python中的变量】
【在Python中模拟实现指针】
ptyhong调用DLL,怎么使用结构体数组指针做参数ptyhong调用DLL,如何使用结构体数组指针做参数
C++函数原型
typedef struct
{
unsigned long DeviceType;
int Handle;
int NumberOfClients;
int SerialNumber;
int MaxAllowedClients;
}NeoDevice;
int _stdcall icsneoFindNeoDevices(unsigned long DeviceTypes,NeoDevice *pNeoDevices, int *pNumberOfDevices);
使用python如下:
class NeoDevice(Structure):
_fields_ = [("DeviceType",c_ulong),
("Handle",c_int),
("NumberOfClients",c_int),
("SerialNumber",c_int),
("MaxAllowedClients",c_int)]
class cNeoVICan(CCanBase):
def __init__(self):
neoVi = windll.icsneo40
self.icsneoFindNeoDevices = neoVi.icsneoFindNeoDevices
if __name__ == "__main__":
canBus = cNeoVICan()
print canBus.icsneoGetDLLVersion()
iNumberOfDevices = [NeoDevice() for x in range(10)]
num = c_int
iResult = canBus.icsneoFindNeoDevices(c_ulong(65535), pointer(iNumberOfDevices), byref(num))
但是会报如下错误:
Traceback (most recent call last):
File "C:\Work\Project\GUI\wxPyCANC303\Drv\source\src\drv\neoVI\cNeoVICan.py", line 224, in module
iResult = canBus.icsneoFindNeoDevices(c_ulong(65535), pointer(iNumberOfDevices), byref(num))
TypeError: _type_ must have storage info
请问是什么错误原因?。?
谢谢 。
------解决方案--------------------
因为python的list不是一个ctypes类型
正确做法是
class NeoDevice(Structure):
_fields_ = [("DeviceType",c_ulong),
("Handle",c_int),
("NumberOfClients",c_int),
("SerialNumber",c_int),
("MaxAllowedClients",c_int)]
class cNeoVICan(CCanBase):
def __init__(self):
neoVi = windll.icsneo40
self.icsneoFindNeoDevices = neoVi.icsneoFindNeoDevices
【python函数指针数组 指针python数据结构】if __name__ == "__main__":
canBus = cNeoVICan()
print canBus.icsneoGetDLLVersion()
iNumberOfDevices = (NeoDevice * 10)()
num = c_int()
iResult = canBus.icsneoFindNeoDevices(c_ulong(65535), cast(iNumberOfDevices, POINT(NeoDevice)), byref(num))
python 调用c++程序, c++程序如何返回数组给pythonC/C++不能直接返回一个数组 。这是由于在C/C++中,数组不是一种类型,因此不能被直接返回 。
一般有两种方法来返回一个数组 。

推荐阅读