-
I asked ChatGPT, and here is the answer. Is this the best way to do it? If my struct has many fields, it might be a bit verbose to create separate getter and setter methods for each one. If your C API uses pointers, you’ll get back and pass around opaque // mylib2.h
typedef struct { int a, b; } Pair;
// allocate on the heap
Pair* new_pair(int a, int b);
// read fields by value
int get_a(Pair* p); You’d import as: LIB = "libmylib2.so"
from C import LIB.new_pair(int, int) -> cobj as new_pair
from C import LIB.get_a(cobj) -> int as get_a
p = new_pair(3, 4) # p is a cobj pointing to a Pair
print(get_a(p)) # reads the 'a' field |
Beta Was this translation helpful? Give feedback.
Answered by
arshajii
Jun 8, 2025
Replies: 1 comment
-
You can usually just replicate the structure definition in Codon: @tuple
class Pair:
a: i32
b: i32 Then you can use LIB = "libmylib2.so"
from C import LIB.new_pair(i32, i32) -> Ptr[Pair] as new_pair # note that C `int` == Codon `i32`
p = new_pair(i32(3), i32(4))
print(p[0].a) # reads the 'a' field -- use int(..) to convert i32 to standard int |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
qinwf
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can usually just replicate the structure definition in Codon:
Then you can use
Ptr[Pair]
: