gpt4 book ai didi

python - 痛饮,py + C : no attribute under pointer

转载 作者:太空宇宙 更新时间:2023-11-04 03:03:10 25 4
gpt4 key购买 nike

我对 SWIG ( C + python ) 有一个奇怪的问题

在 C 中,我有一个返回指向结构 elements_t 的指针的函数。我在 python 中调用函数,得到结果(指针)但我无法访问结构的元素..

typedef struct elements elements_t;
struct elements {
int nelements;
// ... other stuff
};

elements_t* get_elements()
{
elements_t* p;
// ...
return p;
}

在 python 中我这样做:

r = clibrary.get_elements()
print r
# <Swig Object of type 'elements_t *' at 0xb77029f8>
print r.nelements
# AttributeError: 'SwigPyObject' object has no attribute 'nelements'

所以我得到最后一个错误,没有nelements,即使p 指向正确的结构...

最佳答案

根据pointers Python 文档的 SWIG 部分:

The only thing you can't do is dereference the pointer from Python.

您需要取消引用该指针才能访问其成员。您需要在 C 中编写访问器/修改器函数来操作结构成员。

关于python - 痛饮,py + C : no attribute under pointer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9044768/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com