Golang CGo: converting union field to Go type

后端 未结 4 1624
深忆病人
深忆病人 2021-01-12 04:55

I\'m working with this C struct on a 64 bit platform, trying to access the ui32v field in the value union:

struct _GNetSnmp         


        
4条回答
  •  生来不讨喜
    2021-01-12 05:41

    Sonia already answered her own question, I just want to provide the reason for why two type conversions are necessary.

    From the documentation for unsafe.Pointer:

    1) A pointer value of any type can be converted to a Pointer.

    2) A Pointer can be converted to a pointer value of any type.

    3) A uintptr can be converted to a Pointer.

    4) A Pointer can be converted to a uintptr.

    Since var ptr uint64 is not a pointer (as type uint64 is not a pointer), ptr cannot be converted directly to unsafe.Pointer using rule 1. Therefore it is necessary to first convert ptr to uintptr, then from uintptr to a Pointer following rule 3.

提交回复
热议问题