Matthew Bedford Matthew Bedford - 1 month ago 30
C Question

Julia - fixed size array in C struct

I need to make a Julia type corresponding to a C struct that has a fixed size array:

struct cstruct {
...
int arr[N] //N known at compile time
...
};


I have defined Julia types corresponding to other C structs with arrays like this:

type jstruct
...
arr::Ptr{Cint}
...
end


But as I understand it, this only works when
arr
is a pointer, not an array of a specific size. How can I ensure that the offsets of elements coming after
arr
remain the same in both languages?

Answer

When you define a C struct with a fixed size array (or with the array hack), the data are stored directly inline within that struct. It's not a pointer to another region. The equivalent Julia structure is:

type JStruct{N}
    arr::NTuple{N,Int}
end

That will store the integers directly inline within the struct.