Julia中的C结构类型对应

Julia中的C结构类型对应,c,struct,julia,C,Struct,Julia,我试着用Julia中的C函数和结构作为参数,我调用了这些函数,但出现了一些问题。下面是一个简单的例子: 在C语言中: typedef struct { int width; int height; int stride; float* elements; } Matriz; immutable Matriz width::Cint height::Cint stride::Cint elements::Array{Float32,1} end M=Matriz

我试着用Julia中的C函数和结构作为参数,我调用了这些函数,但出现了一些问题。下面是一个简单的例子:

在C语言中:

typedef struct {
    int width;
    int height;
    int stride;
    float* elements;
} Matriz;
immutable Matriz
width::Cint
height::Cint
stride::Cint
elements::Array{Float32,1}
end

M=Matriz(5,5,5,Array{Float32,1}(collect(0:24))) #creating a Matrix of 5x5


ccall((:GetElm,"path/to/dll"),Float32,(Matriz,Cint,Cint),M,0,1)
是存储矩阵的简单结构,具有
宽度
高度
跨距
元素
字段

float GetElm(const Matriz A, int row, int col)
{
    return (row < A.height && col < A.width ?
        A.elements[row * A.stride + col] : 0);
}

ccall
应根据代码返回
1.0
,但返回另一个值,是否有我做错了什么?

当您使用
ccall
直接使用数组参数进行函数调用时,Julia发送
数组的本机地址

然而,对于
struct
s,该过程不是自动的。仍然可以发送数组本机地址的一种方法是使用
指针
。如果要使用此方法,需要在Julia中更新
结构

struct Matriz
    width::Cint
    height::Cint
    stride::Cint
    elements::Ptr{Cfloat}
end

arr = Array{Float32,1}(collect(0:24))
M=Matriz(5,5,5, pointer(arr)) #creating a Matrix of 5x5
ccall((:GetElm,"mylib.so"),Float32,(Matriz,Cint,Cint),M,0,1)
在进行此操作之前,请阅读本手册,因为这是一项不安全的操作,原因是Julia GC