慕的地6264312
编辑这将打印正确的内容package main/*#include <stdio.h>void getMatrix(const float **matrix){ float *m = (float *)matrix; int i; for(i = 0; i<9; i++) { printf("%f\n",m[i]); }}*/import "C"import "unsafe"func main() { a := []float32{1,2,3,4,5,6,7,8,9} C.getMatrix((**C.float)(unsafe.Pointer(&a[0])))}
鸿蒙传说
扩展Inuart提供的答案:package main/*#include <stdio.h>void getMatrix(const float **matrix){ float *m = (float *)*matrix; int i; for(i = 0; i<16; i++) { printf("%f\n",m[i]); }}*/import "C"import "unsafe"func main() { // Create the contiguous 16 element array, but organise it how it is described. a := [4][4]float32{ {1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15, 16}, } m := &a // Take the pointer. C.getMatrix((**C.float)(unsafe.Pointer(&m))) // Take the handle and pass it.}这为您提供了您似乎需要的处理行为,并具有Go语言中数据的形状符合C API所要求的优点-无需回避使用的便利性和安全性继续吧,仅因为您正在与C交互。