How to point to pointer struct

248 views
Skip to first unread message

alex martin

unread,
May 23, 2017, 6:39:12 AM5/23/17
to golang-nuts
Hi 
I have this structure in C created by rsl libray 

typedef struct {
  Radar_header h;
  Volume **v;   
} Radar;

Using cgo i read the structure with 

Radar *readRad(char *fname) {
  return RSL_anyformat_to_radar(fname, NULL);
}

and call this function in go 
RPointer := C.readRad(C.CString(os.Args[1]))

and i see the structure
with fmt.Printf("%T",RPointer)    =>  *main._Ctype_struct___0
with fmt.Printf("%+v",RPointer)   =>  &{h:{} _:[0 0 0 0] v:0xe59b60}

The field is a **C.Volume pointer.
The question is: it's  possible with Golang list all C.Volume's struct pointed ?

like this   

for i := 0; i < int(RPointer.h.nvolumes); i++ {
			pv := *RPointer.v[i]
fmt.Printf("\nVOLUME Sweep %+v\n", *pv.sweep) }


the exit is type **C.struct___2 does not support indexing

I tried with unsafe.Pointer using 

puint1 := unsafe.Pointer(RPointer.v)
sizep := unsafe.Sizeof(RPointer.v)
fmt.Printf("\npuint [%+v] -> + sizeoff [%+v]\n", puint1, unsafe.Pointer(uintptr(puint1)+sizep))


and printf is puint [0xe59b60] -> + sizeoff [0xe59b68]

there is a casting like this 

volume2 := (*C.Volume)(*unsafe.Pointer(uintptr(puint1) + sizep))

Thanks
Alex



Pablo Rozas Larraondo

unread,
May 23, 2017, 9:42:26 AM5/23/17
to golang-nuts
I'm thinking about the possibility of having something like [][]Volume in Go. Do you know the size or number of Volumes in your struct?

There's an example of converting **char into []string that might be useful:

alex martin

unread,
May 23, 2017, 4:05:11 PM5/23/17
to golang-nuts
Thank you
problem solved with your suggestion.

var PSlv (**C.Volume) = RPointer.v
lenght := int(RPointer.h.nvolumes)
sliceV := (*[1 << 30](*C.Volume))(unsafe.Pointer(PSlv))[:lenght:lenght]

the result is  []*C.Volume correct, where the pointer is null the slice value is nil

Alex
Reply all
Reply to author
Forward
0 new messages