Search code examples
cgocgo

Using cgo with typedef variables


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)


Solution

  • 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))