On Monday, December 21, 2015 at 9:42:30 AM UTC-8, Ian Lance Taylor wrote:
On Sat, Dec 19, 2015 at 4:04 PM, Tim K <tim....@gmail.com <javascript:>>
I was pretty sure that for the following type conversions I would have to go
through an intermediate type conversion to unsafe.Pointer, but to my
beginner surprise this works:

var x [5]byte
C.f1( (*C.uint8_t)(&x[0]), 5, ... )
C.f2( (*C.uchar)(&x[0]), 5, ... )

Is it because C.uint8_t and C.uchar are aliases for byte? Something else?
And I assume it's safe to pass a Go byte buffer like this to a C
function to
directly fill it out with data, correct? I don't need to C.malloc (and later
C.free) a C buffer and then copy from it or use C.GoBytes
It's because Go permits conversions between unnamed types as long as
the underlying types are identical. In this case the unnamed types
are *C.uint8_t (and *C.uchar) and *byte. The underlying type of both
C.uint8_t and C.uchar is uint8, which is also the underlying type for
byte. So *C.uint8_t and *byte have identical underlying types, and
the conversion is permitted.
As usual, thank you Ian for reminding me about unnamed types.

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+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Search Discussions

Discussion Posts


Related Discussions

Discussion Navigation
viewthread | post
posts ‹ prev | 5 of 5 | next ›
Discussion Overview
groupgolang-nuts @
postedDec 20, '15 at 12:04a
activeDec 22, '15 at 8:51p



site design / logo © 2022 Grokbase