# Julia call c struct function

**URL:** <https://discourse.julialang.org/t/julia-call-c-struct-function/83699>\
**Category:** General Usage\
**Tags:** ccall, c\
**Created:** [July 3, 2022, 12:38pm UTC](https://discourse.julialang.org/t/julia-call-c-struct-function/83699 "2022-07-03T12:38:15Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![kongdd](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kongdd/32/17058_2.png) [@kongdd](https://discourse.julialang.org/u/kongdd)\
**Post date:** [July 3, 2022, 12:38pm UTC](https://discourse.julialang.org/t/julia-call-c-struct-function/83699/1 "2022-07-03T12:38:15Z")

</div>

I need to interface the C language `update_leaf` function with Julia. But the current script has memory error.

```nohighlight
// leaf.c 
// gcc -shared -fPIC -o leaf.o leaf.c
#include <stdio.h>

/** second solution */
typedef struct {
    int x;
    double y;
    double z[2];
} Leaf;

#define LEAF(...) ((Leaf){.x = 0, .y = 0, .z = {0.0, 1.0}, ## __VA_ARGS__ })

void init_leaf_obj(Leaf obj[]) {
  obj->x = 1;
  obj->y = 2;

  obj->z[0] = 1;
  obj->z[1] = 2;
} 

Leaf init_leaf(int a) {
    Leaf x;
    init_leaf_obj(&x);
    return x;
}

void update_leaf(Leaf obj[], double val) {
    // Leaf x;
    // init_leaf_obj(&x);
    obj->y = val;
    // obj->z[0] = val;
    // obj->z[1] = val;
    // return x;
}

int main() {
  printf("hello");  
  Leaf x = init_leaf(1);

  return 1;
}

```

```julia
using Libdl
# include("test/Soil.jl")
# p = Soil()

mutable struct Leaf_1
  x::Cint
  y::Cdouble
  z::NTuple{2,Cdouble}
end

mutable struct Leaf
  x::Cint
  y::Cdouble
  z::Ptr{Cdouble}
end

libpath = "leaf_1.o"
run(`gcc -shared -fPIC -o $libpath leaf.c`)

leaf = ccall((:init_leaf, libpath),
  Leaf_1, (Cint,), Cint(1))

x = [leaf]
ccall((:update_leaf, libpath),
  Leaf,
  (Ptr{Leaf}, Cdouble),
  x, Cdouble(99.0))

leaf

```

 ![MGZJCWWZB$XEL})M23RZU](https://global.discourse-cdn.com/julialang/original/3X/0/c/0c138ccc62ee7695fe1ba7d5902faef2773001c5.jpeg)

Any idea how to solve?

---

<div class="post-metadata">

**Author:** ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)\
**Post date:** [July 3, 2022, 1:24pm UTC](https://discourse.julialang.org/t/julia-call-c-struct-function/83699/2 "2022-07-03T13:24:59Z")

</div>

The code you pasted works as is but that is not the same code as in your screenshot. I removed some unsed things, but this works:

```nohighlight
// leaf.c
#include <stdio.h>

typedef struct {
    int x;
    double y;
    double z[2];
} Leaf;

void init_leaf_obj(Leaf obj[]) {
  obj->x = 1;
  obj->y = 2;
  obj->z[0] = 1;
  obj->z[1] = 2;
}

Leaf init_leaf(int a) {
    Leaf x;
    init_leaf_obj(&x);
    return x;
}

void update_leaf(Leaf obj[], double val) {
    obj->y = val;
}

```

```julia
# leaf.jl
using Libdl

libpath = abspath("leaf.so")
run(`gcc -shared -fPIC -o $libpath leaf.c`)

mutable struct Leaf
  x::Cint
  y::Cdouble
  z::NTuple{2,Cdouble}
end

leaf = ccall((:init_leaf, libpath), Leaf, (Cint,), 1)

ccall((:update_leaf, libpath), Cvoid, (Ptr{Leaf}, Cdouble), Ref(leaf), 99.0)

```

---

<div class="post-metadata">

**Author:** ![kongdd](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kongdd/32/17058_2.png) [@kongdd](https://discourse.julialang.org/u/kongdd)\
**Post date:** [July 3, 2022, 7:06pm UTC](https://discourse.julialang.org/t/julia-call-c-struct-function/83699/3 "2022-07-03T19:06:25Z")

</div>

> [@fredrikekre](#):
>
> ```julia
> # leaf.jl
> using Libdl
> 
> libpath = abspath("leaf.so")
> run(`gcc -shared -fPIC -o $libpath leaf.c`)
> 
> mutable struct Leaf
> x::Cint
> y::Cdouble
> z::NTuple{2,Cdouble}
> end
> 
> leaf = ccall((:init_leaf, libpath), Leaf, (Cint,), 1)
> 
> ccall((:update_leaf, libpath), Cvoid, (Ptr{Leaf}, Cdouble), Ref(leaf), 99.0)
> 
> ```

Thank you @fredrikekre. Thank for your solution.  
In my screenshot, it really not work. But now it is different. It is little weird.
