Hi all,

I am trying to access members of a C struct from go. Compiler is showing 
structure's members are undefined. I have attached my code and the error 
message below. 

Please have a look and help if you can.

*$CGO_ENABLED=1 go run main.go cfuncs.go*


*# command-line-arguments./main.go:18:44: baz_ptr.b undefined (type *GoBaz 
has no field or method b)./main.go:19:44: baz_ptr.f undefined (type *GoBaz 
has no field or method f)*

//c_header.h
typedef void (*cb)(void* qux, int status, void *command_data, void 
*cb_data);
typedef struct{
  int a;
  int b;
} Foo;

typedef struct{
  char* ch;
  cb callback;
} Bar;

typedef struct{
  Foo* f;
  Bar* b;
} Baz;
//--------------------------------

//cfuncs.go
package main
/*
#include<c_header.h>
#include <stdlib.h>

Baz* allocateBaz(){
     Baz* baz = malloc(sizeof(Baz));
     Bar* bar = malloc(sizeof(Bar));
     Foo* foo = malloc(sizeof(Foo));
     baz->f = foo;
     baz->b = bar;
     return baz;
}
*/
import "C"
//-------------------------------------------------

//main.go
package main/*
#include<c_header.h>
extern Baz* allocateBaz();
*/
import "C"
import "unsafe"
type GoBaz C.struct_Baz
type GoBar C.struct_Bar
type GoFoo C.struct_Foo
func main() {
  baz_ptr := (*GoBaz)(unsafe.Pointer(C.allocateBaz()))
  bar_ptr := (*GoBar)(unsafe.Pointer(baz_ptr.b))
  foo_ptr := (*GoFoo)(unsafe.Pointer(foo_ptr.f))
} 
//--------------------------------
Thanks in advance!
Regards
Ami

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/a3b04162-ccbc-413d-9181-a2c18e5b2277n%40googlegroups.com.

Reply via email to