I have an array of strings in Python 2.7, which I would like to pass to a C function via ctypes:
unsigned int SetParams(unsigned int count, const char **params)
So I can define the parameters in python:
import ctypes as ct
lib = ct.cdll.LoadLibrary('...')
lib.SetParams.restype = ct.c_uint
lib.SetParams.argtypes = [ct.c_uint, ct.POINTER(ct.c_char_p)]
but now when I am given a set of params in a Python function, which I would like to use in the above library call, how do I actually call it? I am thinking something like this:
def setParameters(strParamList):
numParams = len(strParamList)
strArrayType = ct.c_char_p * numParams
strArray = strArrayType()
for i, param in enumerate(strParamList):
strArray[i] = param
lib.SetParams(numParams, strArray)
I am just starting with ctypes and wondering if there is a more automated way to do this? Also is the assignment strArray[i] = param actually reallocating? If so, this seems quite expensive -- is there a way to do this just pointing the string pointers to Python-allocated buffers, or they are not NULL-terminated?
UPDATE I did look through a couple related questions, but could not find a direct one to deal with the same issues. Thank you very much