Currently I’m using Cgo to call C functions from Go. I’m trying to recreate the ‘Read a Photo’ example in Go.
One on the C functions, however, expects a int* len argument (bonus question; is that the same as int *len?). As I read this, this is a pointer to an integer. The function in question is ccv_write of the libccv library. It’s full signature is:
int ccv_write(ccv_dense_matrix_t* mat, char* out, int* len, int type, void* conf)
The relevant code snippet is as follows:
type Image struct {
image *C.ccv_dense_matrix_t
}
func main() {
image := new(Image)
/* ... snip ... */
dst := C.CString("black_and_white_painting.jpg")
defer C.free(unsafe.Pointer(dst))
x := 0 // <- perhaps var x int ?
C.ccv_write(image.image, dst, (*C.int)(&x), C.CCV_IO_PNG_FILE, 0)
}
The above example generates the following compile time error: cannot convert &x (type *int) to type *_Ctype_int
Any thoughts on how to pass the correct argument?
The issue here is you have two different types. You have an
intwhich is defined by your Go compiler andC.intwhich is defined by your C compiler. Depending on the compilers and architecture these may be the same size. However, it is very possible thatintwill be 64 bits in size andC.intwould be 32 bits.In order for the compiler to enforce the type safety, it requires you to do an explicit conversion or define the type at initialization.
In your example, you do:
x := 0. This is the same asx := int(0). Theintis implied. Instead, you can dox := C.int(0).However, I think the “cleaner” method would be to do what jnml recommended:
var x C.int. This does exactly the same thing but just looks nicer.Later, you will most likely want to convert that
C.intback to anint. This is easy to do with a conversion.This will convert
xto anintand then copy that value into the newly initialized variabley.Bonus Anwser:
int* lenis indeed the same asint *len