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
感謝您的回答。我看起來很好。我會盡快嘗試。 –