I need to pass slice of structure objects to C function. C function expects pointer to struct objects I followed How to pass pointer to slice to C function in go. I tried to replicate the original requirement in sample. In sample I am getting
could not determine kind of name for C.f
I am C programmer, just started working on Go-module of project. Can someone correct the below sample or provide a sample to pass go slice to C-function (C-code takes pointer to structure or double pointer (whatever is appropriate))
here is my sample code
package main
/*
#include <stdio.h>
#include "cgoarray.h"
struct test {
int a;
int b;
};
int f(int c, struct test **s) {
int i;
printf("%d\n", c);
for (i = 0; i < c; i++) {
printf("%d\n", s[i].a);
}
c = (c) + 1;
return 1;
}
*/
import "C"
import "unsafe"
type struct gotest{
a int
b int
}
func go_f(harray ...gotest) {
count := len(harray)
c_count := C.int(count)
cArray :=(*C.struct_test)(C.malloc(C.size_t(c_count) *8));
// convert the C array to a Go Array so we can index it
a := (*[1<<30 - 1]*C.struct_test)(cArray)
for index, value := range harray {
a[index] = value
}
err := C.f(10, (**C.struct_test)(unsafe.Pointer(&cArray)))
return 0
}
func main(){
t :=gotest{10,20}
t1 :=gotest{30,40}
t2 :=gotest{50,60}
fmt.Println(t,t1,t2)
go_f(t1,t2,t3)
}