这个go语言封装c函数,使用cgo,参数怎么传?

需要用go语言把下列的c函数封装一下,c的函数如下:
int raw_to_span(OPResult result, int *num, int **time, short **status, double **value);

下面是我的写法,不正确,编译不通过:
func RawToSpan(result uintptr) (num int, time []int, status []int16, value []float64, ret int) {
var t = make([]C.int, num)
var s = make[]C.short, num)
var v = make([]C.double, num)
var n C.int
ret = int(C.raw_to_span(C.OPResult(result), &n, &t, &s, &v))
k := C.op2_num_rows(C.OPResult(result))
num = int(n)
for j := 0; j < int(k); j++ {
time = append(time, int(t[j]))
status = append(status, int16(s[j]))
value = append(value, float64(v[j]))
}
return
}
编译结果:
.\api.go:533: cannot use &t (type *[]C.int) as type **C.int in argument to _Cfunc_op2_raw_to_span
.\api.go:533: cannot use &s (type *[]C.short) as type **C.short in argument to _Cfunc_op2_raw_to_span
.\api.go:533: cannot use &v (type *[]C.double) as type **C.double in argument to _Cfunc_op2_raw_to_span

我也尝试了将t,s,v声明为:var t *C.int 这样写的确是在调用c的时候可以传进去了,但是返回值的时候怎么办?

在线等解答,谢谢
更多 0

1个回答

需要用go语言把下列的c函数封装一下,c的函数如下:
int raw_to_span(OPResult result, int *num, int **time, short **status, double **value);

下面是我的写法,不正确,编译不通过:
func RawToSpan(result uintptr) (num int, time []int, status []int16, value []float64, ret int) {
var t = make([]C.int, num)
var s = make[]C.short, num)
var v = make([]C.double, num)
var n C.int
ret = int(C.raw_to_span(C.OPResult(result), &n, &t, &s, &v))
k := C.op2_num_rows(C.OPResult(result))
num = int(n)
for j := 0; j < int(k); j++ {
time = append(time, int(t[j]))
status = append(status, int16(s[j]))
value = append(value, float64(v[j]))
}
return
}
编译结果:
.\api.go:533: cannot use &t (type *[]C.int) as type **C.int in argument to _Cfunc_op2_raw_to_span
.\api.go:533: cannot use &s (type *[]C.short) as type **C.short in argument to _Cfunc_op2_raw_to_span
.\api.go:533: cannot use &v (type *[]C.double) as type **C.double in argument to _Cfunc_op2_raw_to_span

我也尝试了将t,s,v声明为:var t *C.int 这样写的确是在调用c的时候可以传进去了,但是返回值的时候怎么办?

在线等解答,谢谢

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐