0
我一直在试图从C++返回一个字符串数组蟒蛇一个如下:如何从C++返回char **并使用ctypes将其填充到Python列表中?
// c++ code
extern "C" char** queryTree(char* treename, float rad, int kn, char*name, char *hash){
//.... bunch of other manipulation using parameters...
int nbr = 3; // number of string I wish to pass
char **queryResult = (char **) malloc(nbr* sizeof(char**));
for (int j=0;j<nbr;j++){
queryResult[j] = (char *) malloc(strlen(results[j]->id)+1);
if(queryResult[j]){
strcpy(queryResult[j], "Hello"); // just a sample string "Hello"
}
}
return queryResult;
}
// output in C++:
Hello
Hello
Hello
以下是在Python代码:Python中
libtest = ctypes.c_char_p * 3;
libtest = ctypes.CDLL('./imget.so').queryTree("trytreenew64", ctypes.c_float(16), ctypes.c_int(20), ctypes.c_char_p(filename), ctypes.c_char_p(hashval))
print libtest
输出为整数?
我是python的新手。我知道我在Python方面做错了什么。我一直在看一些其他的问题,他们通过一个char *,但我无法得到它为char **工作。我试了几个小时。任何帮助,将不胜感激。
19306416
@eryksun - 是的,这应该是指针......我错过了。至于'libtest',我只是希望脚本以与原始问题相同的变量结束。 – tdelaney
感谢您的回答。我看起来很好。我会尽快尝试。 –