title: Generalized function to pass array of char from C to Go
body: <p>I am trying to copy content of structure in Go</p>
  
  <pre><code>type test struct {
      a string
      b string
  }
  </code></pre>
  
  <p>to C structure. </p>
  
  <pre><code>typedef struct {
      char a[10];
      char b[75];
  }test_c;
  </code></pre>
  
  <p>My requirement does not allow me to change the structure of member types of both Go and C. I have written the following code, to copy Go structure to C structure, and i am able to copy them too.</p>
  
  <blockquote>
    <p>cmain.h</p>
  </blockquote>
  
  <pre><code>typedef struct {
      char a[10];
      char b[75];
  }test_c;
  void cmain(test_c* value);
  </code></pre>
  
  <blockquote>
    <p>cmain.c</p>
  </blockquote>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include "cmain.h"
  #include "_cgo_export.h"
  
  void cmain(test_c *value) {
      printf("Inside C code\n");
      printf("C code structure Member:a=%s\n", value-&gt;a);
      printf("C code structure Member:b=%s\n", value-&gt;b);
  }
  </code></pre>
  
  <blockquote>
    <p>go_func.go</p>
  </blockquote>
  
  <pre><code> package main
   import (
      /*
          #include "cmain.h"
      */
      "C"
      "fmt"
      "unsafe"
  )
  
  type test struct {
      a string
      b string
  }
  
  func main() {
  
      var p_go test
      var p_c C.test_c
      p_go.a = "ABCDEFGHIJ"
      p_go.b = "QRSTUVXWYZ"
      fmt.Println(unsafe.Sizeof(p_c.a))
      StructMemberCopy(&amp;p_c.a, p_go.a)
      fmt.Println("In GO code\n")
      fmt.Println("GO code structure Member:a=%s", p_go.a)
      fmt.Println("GO code structure Member:b=%s", p_go.b)
  
      fmt.Println("Call C function by passing GO structure\n")
      C.cmain((*C.test_c)(unsafe.Pointer(&amp;p_c)))
  
  }
  
  func StructMemberCopy(dest *[10]C.char, src string) {
      for i, _ := range src {
          dest[i] = C.char(src[i])
          fmt.Printf("%d %c \n", i, src[i])
      }
  }
  </code></pre>
  
  <p>Here am trying to write a generalized function to copy the content of Go Structure member to C Structure  </p>
  
  <pre><code>func StructMemberCopy(dest *[10]C.char, src string) {
          for i, _ := range src {
              dest[i] = C.char(src[i])
              fmt.Printf("%d %c \n", i, src[i])
          }
      }
  </code></pre>
  
  <p>In the above code, when i call the function <strong>StructMemberCopy(&amp;p_c.a, p_go.a)</strong>, with &amp;p_c.a as argument the code compiles without any error, but when i send &amp;p_c.b, it gives an error</p>
  
  <pre><code>cannot use &amp;p_c.b (type *[75]C.char) as type *[10]C.char in argument to StructMemberCopy
  </code></pre>
  
  <p>Can anyone give some inputs as to how to write the generalized argument for <strong>func StructMemberCopy</strong>, as my C Structure array member size can vary. Mean it can become </p>
  
  <pre><code>typedef struct {
      char a[10];
      char b[75]; 
  }test_c;
  
  or typedef struct {
      char a[15];
      char b[40];
  }test_c;
  </code></pre>