Matthew Bedford
Matthew Bedford

Reputation: 727

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?

Upvotes: 5

Views: 1426

Answers (2)

tholy
tholy

Reputation: 12179

Note that if you want array-type operations on this object in Julia, the StaticArrays package might be useful. It uses tuples to store the elements of arrays, while also giving them an AbstractArray interface.

Upvotes: 4

mbauman
mbauman

Reputation: 31342

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.

Upvotes: 10

Related Questions