title: cgo - How to convert string to C fixed char array
body: <p>I'm trying to instantiate a C struct inside my Go code.
  The struct is defined like this, (in an external library that I cannot modify):</p>
  
  <pre><code>typedef struct {
      char field1[256];
  } S1
  </code></pre>
  
  <p>In go, I did this:</p>
  
  <pre><code>func myfunc(mystr string){
      // We need to convert mystr from string to char array
      cStr := C.CString(mystr)
      defer C.free(unsafe.Pointer(cStr)
  
      // Should work now
      s1 := &amp;C.S1{field1: cStr}
  
      // Do something with s1...
  }
  </code></pre>
  
  <p>But it doesn't compile because:</p>
  
  <blockquote>
    <p>cannot use cStr (type *C.char) as type [256]C.char in field value</p>
  </blockquote>
  
  <p>I've tried forcing ([256]C.char)(cStr) but it obviously doesn't work either.</p>
  
  <p>Is there a way to achieve what I'm trying to do?</p>