title: How can I use ctypes to pass a byteArray into a C function that takes a char* as its argument?
body: <p>I have created a function in C which takes an int size and a char *buffer as arguments. I would like to use ctypes to call this function from python and pass in a python byteArray. I know that first you must compile the C file into a shared library (.so file) and use ctypes to call that function. Here's the code I have so far. </p>
  
  <p>encrypt.c:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  void encrypt(int size, unsigned char *buffer);
  void decrypt(int size, unsigned char *buffer);
  
  void encrypt(int size, unsigned char *buffer){
      for(int i=0; i&lt;size; i++){
          unsigned char c = buffer[i];
          printf("%c",c);
      }
  }
  void decrypt(int size, unsigned char *buffer){
      for(int i=0; i&lt;size; i++){
          unsigned char c = buffer[i];
          printf("%c",c);
      }
  }
  </code></pre>
  
  <p>And here's the python file:</p>
  
  <pre><code>import ctypes
  
  encryptPy = ctypes.CDLL('/home/aradhak/Documents/libencrypt.so')
  hello = "hello"
  byteHello = bytearray(hello)
  encryptPy.encrypt(5,byteHello)
  encryptPy.decrypt(5,byteHello)
  </code></pre>
  
  <p>Basically I want to call the C method from python, pass through a python byte array, and have it iterate through the array and print each element</p>