Search code examples
cgocgo

How to manipulate a C character array inside a cgo function?


I have a C function, that is calling a go-Function with an char array parameter. The go-Function has to modify the content of the parameter. How to accomplish this?

void cFunction() {
 char buffer[9] = "aaabbbccc"; // 9 is in this case correct, it is not a null-terminated-string
 goFunction(buffer);
 // buffer shall be modified
}
func goFunction(cBuffer *C.char) {
  // how to modify 3-5?
  //strncpy(cBuffer+3, "XXX")
}

EDIT: to be more precise. I have to implement a callbackfunction, that accepts an outparameter, which I have to manipulate.

  void callback(char outbuffer[9]) {
    goFunction(outbuffer);
  }

as I understand Franks answer, I should do something like

  allocate new go buffer
  convert C buffer to go buffer
  manipulate go buffer
  allocate new C buffer
  convert go buffer to C buffer
  memcpy C buffer into outbuffer

That is too much allocation and conversion for my taste


Solution

  • See the documentation for Turning C arrays into Go slices to get a indexable go slice containing the C data.

    Because you are modifying the C buffer data in place, using the Go slice as a proxy, you can simply pass the same buffer to the callback. Note that using append may allocate a new Go array for your slice, so you need to avoid it and make sure you have sufficient space available in the buffer beforehand.

    func goFunction(cBuffer *C.char, length int) {
        slice := (*[1 << 28]C.char)(unsafe.Pointer(cBuffer))[:length:length]
        // slice can now be modified using Go syntax, without pointer arithmetic
        
        C.callback(cBuffer)
    }