Python 指向引用使用ctypes的指针
我的cpp程序:test.cppPython 指向引用使用ctypes的指针,python,c++,Python,C++,我的cpp程序:test.cpp #include <iostream> using namespace std; extern "C" { void test_2(int &e, int *&f) { e=10; f=new int[e]; for(int i=0;i<e;i++) f[i]=i; } } 输出是10和,但我想得到数字10和一个整数数组,我该怎
#include <iostream>
using namespace std;
extern "C"
{
void test_2(int &e, int *&f)
{
e=10;
f=new int[e];
for(int i=0;i<e;i++)
f[i]=i;
}
}
输出是
10
和
,但我想得到数字10和一个整数数组,我该怎么办?首先将b定义为指针
#coding=utf-8
import ctypes
from ctypes import *
if __name__ == "__main__":
lib = ctypes.CDLL("./test.so")
a = c_int()
b = pointer(c_int())
lib.test_2(
byref(a),
byref(b)
)
for index in range(0,a.value):
print b[index]
int*&f是
不是指向引用的指针(没有这样的东西),而是指向指针的引用。您定义了b=c\u int()
。我想您应该将b
定义为指向int的指针。调用的方式是,让test_2
覆盖由pointer(b)
.Ya,b=pointer(c_int())
创建的临时值,然后以byref(b)
的形式传递。我希望从下拉列表中得到解释。很抱歉,我错误地点击了下拉列表,我想点击upvoter。但是网页给了我一条消息,我不能upvoter。如果你稍微修改一下你的代码,然后给你一个upvoter。
#coding=utf-8
import ctypes
from ctypes import *
if __name__ == "__main__":
lib = ctypes.CDLL("./test.so")
a = c_int()
b = pointer(c_int())
lib.test_2(
byref(a),
byref(b)
)
for index in range(0,a.value):
print b[index]