On Mon, Sep 30, 2019 at 1:01 AM T L <tapir....@gmail.com> wrote:
>
> https://golang.org/cmd/cgo/#hdr-Passing_pointers
>
> When passing a pointer to a field in a struct, the Go memory in question is 
> the memory occupied by the field, not the entire struct. When passing a 
> pointer to an element in an array or slice, the Go memory in question is the 
> entire array or the entire backing array of the slice.
>
> Why do structs and arrays have this difference?

Because it's normal to call a C function with (&a[0], len(a)) to let
the C function examine all elements of the array or slice.  It's not
normal to call a C function with (&s.f) and expect the C function to
examine all elements of the struct; if the C function wants to examine
all elements of a struct, you would pass &s, not &s.f.  But you don't
want to pass &a to a C function; with the way C pointers work, you
want to pass a pointer to an element, not a pointer to the array or
slice.  (Admittedly passing a pointer to an array would work anyhow,
but passing a pointer to a slice would not.)

Ian

-- 
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/CAOyqgcVGUomQ3vYgoqVg0-5vgXNhRQCej_tFjxmWNMoLtPyqxQ%40mail.gmail.com.

Reply via email to