title: Python ctypes: Passing an array of strings
body: <p>I have an array of strings in Python 2.7, which I would like to pass to a C function via <code>ctypes</code>:</p>
  
  <pre><code>unsigned int SetParams(unsigned int count, const char **params)
  </code></pre>
  
  <p>So I can define the parameters in python:</p>
  
  <pre><code>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)]
  </code></pre>
  
  <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:</p>
  
  <pre><code>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)
  </code></pre>
  
  <p>I am just starting with <code>ctypes</code> and wondering if there is a more automated way to do this? Also is the assignment <code>strArray[i] = param</code> 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?</p>
  
  <p><strong>UPDATE</strong> 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</p>