目前我使用Cgo從Go調用C函數。我試圖在Go中重新創建'Read a Photo'示例。CGO; C函數有int指針參數,如何傳遞正確的類型?
然而,一個關於C函數的參數需要int* len
參數(獎勵問題;是否與int *len
?相同)。當我讀到這個時,這是一個指向整數的指針。有問題的函數是libccv庫的ccv_write
。它的完整的簽名是:
int ccv_write(ccv_dense_matrix_t* mat, char* out, int* len, int type, void* conf)
相關的代碼片段如下:
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)
}
上面的例子生成以下編譯時錯誤:cannot convert &x (type *int) to type *_Ctype_int
如何通過任何想法正確的論點?
是,在空白C是很少顯著,'INT * LEN;'和'INT len個*;'是等價的。有理由(在我看來)更喜歡前者,因爲後者看起來像是有前途的東西(即'int * a,b;'是什麼意思?)。 – unwind