dongzhan8620 2018-03-12 19:24
浏览 461
已采纳

将cgo与typedef变量一起使用

I am creating a project which uses cryptography that I coded in C due to existing libraries and speed. When attempting to interface with Cgo, I am having some trouble with typedefs I created in C. One example is below:

typedef unsigned char ec_scalar[32];

I am able to successfully create ec_point variables, and use functions that take in ec_point*. However, whenever I attempt to use functions that are passed with a normal ec_point, I get the error:

cannot use pk2 (type C.ec_scalar) as type *C.uchar in argument to _Cfunc_secret_to_public

I can't seem to find a way where I can easily convert this too. I also don't want to have to refactor my code to take in ec_point*, as ec_point is already a pointer (since it is an array of 32 char)

  • 写回答

1条回答 默认 最新

  • drhs13583567608 2018-03-12 19:34
    关注

    Go's static type system and type safety doesn't let you implicitly convert types, and doesn't let arrays decay into pointers. You will need to convert it manually using the unsafe package:

    (*C.uchar)(unsafe.Pointer(pk2))
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!