Search code examples
cgocgo

How do I pass a **void pointer to C function? I am getting a cgo argument has Go pointer to Go pointer


package main

//
// void test(data **void) {
//     Do something here...
// }
import "C"

import "unsafe"

func main() {
    var data *C.void 
    cData := unsafe.Pointer(&data)
    C.test(cData)
}

Above is an example of what I am trying to do but I am receiving a run time error stating cgo argument has Go pointer to Go pointer. How do I resolve this issue? any ideas?


Solution

  • The Go equivalent of void* is an unsafe.Pointer, there's no reason to declare a value of *C.void. The error from your example is not about a Go pointer to a Go pointer (which is a runtime error), you will get an error of (variable of type unsafe.Pointer) as *unsafe.Pointer, which cannot compile. Use the type shown in the error:

    func main() {
        var data unsafe.Pointer
        C.test(&data)
    }