I need to call a C function which takes an argument of type void**
from my Go code. What is the Go equivalent of void**
? And how to properly pass it to the C function?
Asked
Active
Viewed 418 times
1 Answers
0
Well, I found out that void**
is *unsafe.Pointer
, and the unsafe.Pointer
itself is void*
.

zergon321
- 210
- 1
- 10
-
2You've found the right *type*. Getting the right *value* in the `void *` to which the `void **` points is another matter. If the C function you're calling *fills in* a `void *`, it provides the right value, but if it *uses* it instead or as well, you need to provide it a correct input. – torek Dec 31 '20 at 05:49
-
@torek well, I have ```c typedef unsigned char byte; void foo(void** buffer) { *buffer = (byte*)malloc(32); for (int i = 0; i < 32; i++) { byte* temp = (byte*)*buffer; temp[i] = i; } } ``` And in my **Go** function I have `var buffer []byte`. How to pass the buffer to the function above so it could fill the buffer? – zergon321 Dec 31 '20 at 06:06
-
OK, you have the C function *fill in* the `void *` with a pointer to C memory, so you're good to go on the C side. However, the *Go* code wants a slice, and that's a bit of a problem because Go slices normally point to Go-allocated backing arrays. There are standard answers to that question, though; to see links to many of them, start [here](https://stackoverflow.com/q/61961793/1256452). – torek Dec 31 '20 at 06:35