Passing string to a c-struct member of type void*

61 views
Skip to first unread message

Nicholas Yue

unread,
Jan 28, 2021, 8:20:41 PM1/28/21
to golang-nuts
Hi,

  I have the following struct in C
======================
struct DataStruct
{
    const char *name;
    const void *data;
    int type;
    int arraylength;
    size_t count;
    int flags;
};
======================

  I am trying to pass a string/(char *) to data. What is the correct approach ? I believe the other parameters are being passed successfully

My attempt

======================
var params [2]C.struct_DataStruct
params[0].name = C.CString("type")
// cstr := C.CString("apistream")
// params[0].data = unsafe.Pointer(&cstr)
params[0]._type = C.int(0)
params[0].arraylength = C.int(0)
params[0].flags = C.int(0)
======================

Cheers

Alex

unread,
Jan 29, 2021, 10:31:24 AM1/29/21
to golang-nuts
params[0].data = unsafe.Pointer(cstr)

Note no & cuz cstr is already a pointer and &cstr is a pointer to a pointer

Reply all
Reply to author
Forward
0 new messages