Problem in printing array of char pointer passing from Python
My following C code works quite well, till my Python code trying to pass an array of char pointer to it.
The output I obtain is
The file_name is python-file
Another 3 string is not being printed out. Anything I had missed out?
C Code
#include <iostream>
#include "c_interface.h"
int foo(const char* file_name, const char** names) {
std::cout << "The file_name is " << file_name << std::endl;
while (*names) {
std::cout << "The name is " << *names << std::endl;
names++;
开发者_运维百科 }
return 0;
}
/*
int main() {
const char *c[] = {"123gh", "456443432", "789", 0};
foo("hello", c);
getchar();
}
*/
Python Code
#!c:/Python27/python.exe -u
from ctypes import *
name0 = "NAME0"
name1 = "NAME1"
name2 = "NAME2"
names = ((c_char_p * 1024) * 4)()
names[0].value = name0
names[1].value = name1
names[2].value = name2
names[3].value = 0
libc = CDLL("foo.dll")
libc.foo("python-file", names)
Using and compiling your C++ code, I can only repeat the code I already stated in my last answer:
In [1]: import ctypes
In [2]: lib = ctypes.CDLL("libfoo.so.1.0")
In [3]: names = (ctypes.c_char_p*4)()
In [4]: names[0] = "NAME0"
In [5]: names[1] = "NAME1"
In [6]: names[2] = "NAME2"
In [7]: names[3] = 0
In [8]: lib.foo("whatever", names)
The file_name is whatever
The name is NAME0
The name is NAME1
The name is NAME2
Out[8]: 0
As a suggestion for you, open up your Python/IPython shell, execute your line
names = ((c_char_p * 1024) * 4)()
...and check the first element names[0]
directory entry, using dir
. Or, try to access the value attribute for a start.
精彩评论