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))
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大