how can I do a "pointer-cast" for a "cgo" code

2,313 views
Skip to first unread message

gnu.bash

unread,
Nov 16, 2010, 4:23:10 AM11/16/10
to golang-nuts
Hi,

I want to use the function "C.GoStringsN"
this function expect a "*_Ctype_char" argument on first position.
I have an argument "_Ctypedef_MQ_CBI" which is "type _Ctypedef_MQ_CBI
*_Ctype_unsignedchar"

the problem is "char" and "unsigned char". In "C" this is no a problem
to cast
the both pointers but in "GO" I got errors like

6g -o _go_.6 MqS.cgo1.go send.cgo1.go read.cgo1.go _cgo_gotypes.go
read.go:62[read.cgo1.go:64]: cannot use bin.dat (type
_Ctypedef_MQ_CBI) as type *_Ctype_char in function argument

Question: How I can cast pointers in "GO"


mfg, Andreas Otto


My Code:

type Binary struct {
dat C.MQ_CBI
len C.MQ_SIZE
}

func Binary2String(ret uint32, bin Binary) (uint32, string) {
if ret == ERROR {
return ret, ""
}
return ret, C.GoStringN(bin.dat, C.int(bin.len))
}

Russ Cox

unread,
Nov 16, 2010, 1:41:56 PM11/16/10
to gnu.bash, golang-nuts
import "unsafe"
x := (*C.char)(unsafe.Pointer(y))

gnu.bash

unread,
Nov 16, 2010, 1:54:39 PM11/16/10
to golang-nuts
works...

func Binary2String(ret MqSException, bin Binary) (MqSException,
string) {
if ret == ERROR {
return ret, ""
}
return ret, C.GoStringN((*C.char)(bin.dat), C.int(bin.len))
}

thanks
Reply all
Reply to author
Forward
0 new messages