0

I have a C function which will return an array of structure to go function. How can I receive the array of structure and interpret or cast to go structure?

Here is the code snippet

typedef struct student{  
    nameStruct name;  
    addressStruct address;  
} studentStruct;

typedef struct name{
    char firstName[20];
    char lastName[20];
} nameStruct;

typedef struct address{
    char location[40];
    int    pin;
}addressStruct;


student* getAllStudents(){
   //Allocate memory for N number of students
   student *pStudent= (struct student*)(N* sizeof(struct student));
   //populate the array
   return pStudent;
}

I need to get the pStudent array in my go code

package main

/*
#cgo CFLAGS: -I.
#cgo LDFLAGS: -L. -lkeyboard
#include "keyboard.h"
*/
import "C"
import (
    "fmt"
)

type student struct {
    name string
    ID int
}

func main() {
    s := student{} //Need to know how to decide the length of the struct array
    s = C.getAllStudents() //?????


}

Can some one help me with code snippet?

  • There's a lot of missing functionality here, covering a lot of basic areas. What part are you asking about? – JimB Oct 12 '16 at 20:14
  • what is the best way of casting C structure into Go structure? considering there can be different data types in the C structure. – user3218590 Oct 13 '16 at 06:19
  • If the data types are different, you need to copy/convert those to their respective fields. For example, Go strings are not the same as C strings, so you can't "cast" those, you need to copy the characters from the C `char*` to the Go `string` – JimB Oct 13 '16 at 12:52
  • I am aware of that, that's why I was looking for any other short cut which can directly cast or map C struct to Golang struct – user3218590 Oct 14 '16 at 05:27
  • I have added a complex C struct studentStruct in my original post. Can some one add a code snippet to interpret this C struct in Go code? – user3218590 Oct 16 '16 at 15:37
  • What do you mean by "interpret" the C struct? You said that you are aware that you need to copy/convert the data to use native Go structures, but are looking for a shortcut. You can access the struct directly as type `C.struct_student` if that's what you're looking for. – JimB Oct 16 '16 at 18:13
  • Possible duplicate of [cgo: How to pass struct array from c to go](https://stackoverflow.com/questions/28925179/cgo-how-to-pass-struct-array-from-c-to-go) – bain Aug 29 '19 at 13:23

1 Answers1

1

You could use the out paramters, and the way for C struct -> Go struct is thus:

package main

/*
#include <stdlib.h>

typedef struct Point{
    float x;
    float y;
}Point;

void GetPoint(void **ppPoint) {
   Point *pPoint= (Point *)malloc(sizeof(Point));
   pPoint->x=0.5f;
   pPoint->y=1.5f;
   *ppPoint = pPoint;
}
*/
import "C"

import "unsafe"

type Point struct {
    x float32
    y float32
}

func main() {
    var ppoint unsafe.Pointer
    C.GetPoint(&ppoint)
    point := *(*Point)(ppoint)
    println(point.x, point.y)
}
Mohanson
  • 101
  • 1
  • 2