The memory of the structure is already allocated.
I would like to approach C struct in golang.
I want to access a struct variable in golang without the c code, what should I do?
package main
/*
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct
{
int num;
char food[10];
char animal[128];
} sample;
sample *sa;
static void alloc() {
sa = (sample *) malloc (sizeof(sample) * 2);
memset(sa, 0, sizeof(sample) * 2);
sa[0].num = 10;
strcpy(sa[0].food, "noodle");
strcpy(sa[0].animal, "cat");
sa[1].num = 20;
strcpy(sa[1].food, "pizza");
strcpy(sa[1].animal, "dog");
}
*/
import "C"
import "fmt"
func init() {
C.alloc()
}
func main() {
fmt.Println(C.sa[0].num)
fmt.Println(C.sa[0].food)
fmt.Println(C.sa[0].animal)
fmt.Println(C.sa[1].num)
fmt.Println(C.sa[1].food)
fmt.Println(C.sa[1].animal)
}
I have written this example.