Reputation: 241
I use swig wrapped some c++ api function. There is one function, the interface is f(char*[] strs).
How can I pass a valid parameter to this function. This is what I did.
str = ["str","str2"]
f(str)
it will throw error
TypeError: in method 'f', argument 1 of type 'char *[]
Upvotes: 1
Views: 190
Reputation: 29473
SWIG does not convert arrays to Python lists automatically. Since you are using C++, use std::string and std::vector for your f
, then SWIG will make all the necessary conversions automatically (don't forget to include "std_vector.i" and such, see the SWIG docs):
void f(std::vector<std::string> > strs)
If you cannot modify the declaration of f
, you can create an %inline
wrapper in the .i:
%inline {
void f(const std::vector<std::string> >& strs)
{
// create tmpCharArray of type char*[] from strs
const char* tmpCharArray[] = new const char* [strs.size()];
for (i=0; i<strs.size(); i++)
tmpCharArray[i] = strs[i].c_str();
f(tmpCharArray);
delete[] tmpCharArray;
}
}
Upvotes: 1