Julia analogue of C struct?

ccall

#1

C

typedef  struct {
    unsigned  char data [ 64 ];
} my_pubkey;

Julia?


How to convert the data type for ccall
#2

Всем привет!
Как преобразовать тип для ccall?
Вот код на C

typedef  struct {
    unsigned  char data [ 64 ];
} my_pubkey;

that’s what I was trying to do on Julia

struct pubkey
    Cuchar::NTuple{64, Int}
end

private_key = 7

pub = ccall((:secp256k1_ec_pubkey_create, "/usr/local/lib/libsecp256k1.so"), Int, (Pair{Int64, Int64}, Ref{pubkey}, Int32), secp256k1_ctx, pubkey, private_key)


#3

You should use either UInt8 or Cuchar, not Int, in the NTuple, to match the C definition, i.e., also it’s better not to use a Julia type for a field name, i.e.

struct my_pubkey
    data::NTuple{64, Cuchar}
end

will exactly match the C definition.


#4

Thank you!
And how to correctly pass the argument to the pubkey in call?
type of data use nothing?


struct secp256k1_pubkey
    data::NTuple{64, Cuchar}
end

pubkey = secp256k1_pubkey
private_key = 7

pub = ccall((:secp256k1_ec_pubkey_create, "/usr/local/lib/libsecp256k1.so"), Int, (Pair{Int64, Int64}, nothing, Int32), secp256k1_ctx, pubkey, private_key)

function should change pubkey (&pubkey)


#5

You should use triple backquotes, followed by julia, to quote code in Discourse (and end it with a line with triple backquotes).
You are setting pubkey to the type secp256k1_pubkey, not to an instance of it.
I think that to pass to C, you should create a Ref{secp256k1_pubkey}(). (you used to be able to use a &, like C, which was special syntax only for ccall, but I believe that’s been removed).


#6

You should use triple backquotes, followed by julia, to quote code…


#7

Well, I was talking specifically about the code @1112 first posted, of course.
More generally, triple backquotes followed by the name of a (supported) language, such as julia, c, python, etc.


#8

You are right! :slight_smile: I just want to say that you don’t need to specify julia because it seems to be default on this discourse forum.


#9

I hadn’t even realized that you could set a forum specific default… good to know! Thanks!