2018-11-30 00:16

# 如何将C.double数组传递给Cgo函数？

I'm just getting started with CGo and I'm trying to send data to a C library that performs statistical computations on arrays of floats/doubles. What I'm trying to figure out right now is how to send an array of floats, or C.double's, to a CGo function that has a signature like this:

``````double pop_mean(int numPoints, double a[])
``````

I've figured out how to get in the C.int in there, but I'm having trouble figuring out how to send in an array of doubles.

I haven't yet seen any blog posts or SO Questions about this exact thing, so I thought I'd ask.

The following is my best effort so far.

``````// Get a basic function to work, while passing in an ARRAY  arr := make([]C.double, 0)
arr = append(arr, C.double(10.0))
arr = append(arr, C.double(20.0))
arr = append(arr, C.double(30.0))
var fixedArray [3]C.double = arr[:]

// ptr := C.CBytes(arr)
// defer C.free(unsafe.Pointer(ptr))

coolMean := C.pop_mean(3, &fixedArray)
fmt.Println("pop_mean (10, 20, 30): ", coolMean)
``````

And this is the error I'm getting:

``````./main.go:64:6: cannot use arr[:] (type []_Ctype_double) as type [3]_Ctype_double in assignment
./main.go:69:35: cannot use &fixedArray (type *[3]_Ctype_double) as type *_Ctype_double in argument to _Cfunc_pop_mean
``````

How should I be passing an array of C.double to the code?

• 写回答
• 好问题 提建议
• 追加酬金
• 关注问题
• 邀请回答

#### 2条回答默认 最新

• duanmuyao0463 2018-11-30 02:06
最佳回答

When an array name is passed to a function, what is passed is the location of the initial element. Within the called function, this argument is a local variable, and so an array name parameter is a pointer, that is, a variable containing an address.

C Programming Language, 2nd Edition

Slice types

A slice is a descriptor for a contiguous segment of an underlying array and provides access to a numbered sequence of elements from that array.

Like arrays, slices are indexable and have a length. The length of a slice s can be discovered by the built-in function len; unlike with arrays it may change during execution. The elements can be addressed by integer indices 0 through len(s)-1. The slice index of a given element may be less than the index of the same element in the underlying array.

A slice, once initialized, is always associated with an underlying array that holds its elements.

The Go Programming Language Specification

Reference: Go Command cgo

For a slice `a`, the arguments to the `pop_mean(int numPoints, double a[])` C function are `len(a)`, the length of the slice underlying array, and `&a[0]`, the address of the first element of the slice underlying array.

In Go, we often hide details in a function. For example, a `popMean` function,

``````package main

import (
"fmt"
)

/*
double pop_mean(int numPoints, double a[]) {
if (a == NULL || numPoints == 0) {
return 0;
}
double mean = 0;
for (int i = 0; i < numPoints; i++) {
mean+=a[i];
}
return mean / numPoints;
}
*/
import "C"

func popMean(a []float64) float64 {
// This is the general case, which includes the special cases
// of zero-value (a == nil and len(a) == 0)
// and zero-length (len(a) == 0) slices.
if len(a) == 0 {
return 0
}
return float64(C.pop_mean(C.int(len(a)), (*C.double)(&a[0])))
}

func main() {
a := make([]float64, 10)
for i := range a {
a[i] = float64(i + 1)
}

// slice
fmt.Println(len(a), a)
pm := popMean(a)
fmt.Println(pm)

// subslice
b := a[1:4]
fmt.Println(len(b), b)
pm = popMean(b)
fmt.Println(pm)

// zero length
c := a[:0]
fmt.Println(len(c), c)
pm = popMean(c)
fmt.Println(pm)

// zero value (nil)
var z []float64
fmt.Println(len(z), z, z == nil)
pm = popMean(z)
fmt.Println(pm)
}
``````

Output:

``````10 [1 2 3 4 5 6 7 8 9 10]
5.5
3 [2 3 4]
3
0 []
0
0 [] true
0
``````
评论
解决 无用
打赏 举报