# @ccall and pointer to structure

**URL:** <https://discourse.julialang.org/t/ccall-and-pointer-to-structure/121884>\
**Category:** General Usage\
**Created:** [October 28, 2024, 4:43pm UTC](https://discourse.julialang.org/t/ccall-and-pointer-to-structure/121884 "2024-10-28T16:43:28Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![yjr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yjr/32/207725_2.png) [@yjr](https://discourse.julialang.org/u/yjr)\
**Post date:** [October 28, 2024, 4:43pm UTC](https://discourse.julialang.org/t/ccall-and-pointer-to-structure/121884/1 "2024-10-28T16:43:28Z")

</div>

Hello,  
I’ve a c function that updates (pass by parameter) a pointer to a structure. so in @ccall I should use Ptr{my\_struct} types . But I cannot create the structure pointer as method pointer(whatever::MyStruct) does not exist… What is the best way to achieve that ?  
Thks a lot  
Br Yves-Julien

edit: I already have a solution but really not sure it’s the best i.e. using a pointer to vector of Uint8 of corresponding size and reinterpret the binary data to correct types

---

<div class="post-metadata">

**Author:** ![jmert](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jmert/32/3161_2.png) [@jmert](https://discourse.julialang.org/u/jmert)\
**Post date:** [October 28, 2024, 5:02pm UTC](https://discourse.julialang.org/t/ccall-and-pointer-to-structure/121884/2 "2024-10-28T17:02:54Z")

</div>

Use a `Ref` to hold your object, and pass that through the `ccall`:

e.g.

```julia
julia> struct MyStruct
           field::Int
       end

julia> mydata = MyStruct(1)
MyStruct(1)

julia> ref_mydata = Ref(mydata)
Base.RefValue{MyStruct}(MyStruct(1))

julia> @ccall printf("%p\n"::Cstring, ref_mydata::Ptr{MyStruct})::Cint;
0x78e800432b40

```

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [October 28, 2024, 5:05pm UTC](https://discourse.julialang.org/t/ccall-and-pointer-to-structure/121884/3 "2024-10-28T17:05:59Z")

</div>

This section has a good breakdown of when to use `Ref` vs `Ptr:`: [Calling C and Fortran Code · The Julia Language](https://docs.julialang.org/en/v1/manual/calling-c-and-fortran-code/#When-to-use-T,-Ptr%7BT%7D-and-Ref%7BT%7D)

Here’s another very simple example:

```julia
julia> const Ctime_t = Int64
Int64

julia> t = Ref(Ctime_t(0))
Base.RefValue{Int64}(0)

julia> @ccall time(t::Ref{Ctime_t})::Ctime_t
1730135137

julia> t[]
1730135137

julia> @ccall time(t::Ref{Ctime_t})::Ctime_t
1730135140

julia> t[]
1730135140

```

---

<div class="post-metadata">

**Author:** ![yjr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yjr/32/207725_2.png) [@yjr](https://discourse.julialang.org/u/yjr)\
**Post date:** [October 28, 2024, 8:25pm UTC](https://discourse.julialang.org/t/ccall-and-pointer-to-structure/121884/4 "2024-10-28T20:25:02Z")

</div>

Thanks, this works ! I was confused because I tried a more concise way that work only I think if the structure is used as input to the function as below:

```julia
struct MyStruct
           field::Int
       end

julia> mydata = MyStruct(1)
MyStruct(1)

julia> @ccall printf("%p\n"::Cstring, Ref(mydata)::Ptr{MyStruct})::Cint;

```

But if the functions changes the structure, it is not reflected in the julia structure. To my understanding it’s because the pointer is created in the function call thus valid only at function call so the after it is GC and therefore could not alter the content of mydata…  
Am I correct ?

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [October 28, 2024, 8:57pm UTC](https://discourse.julialang.org/t/ccall-and-pointer-to-structure/121884/5 "2024-10-28T20:57:02Z")

</div>

That identifier there — `mydata` — is just a name for _you_ that can serve as a name for the value `MyStruct(1)`. `MyStruct` itself is not mutable; it cannot change. And `mydata` can only become the name of a different thing if you explicitly assign something else to it, deciding you have a better use for that name.

When you put a value in a box like a `Ref`, then you (or your C code) can decide to switch out what’s on the inside _of that box_. If you throw away the box (or just don’t name it anything meaningful), then you won’t be able to observe the change.

In short, Julia’s variables don’t have the same pointer semantics that you might be used to.

---

<div class="post-metadata">

**Author:** ![sgaure](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sgaure/32/14779_2.png) [@sgaure](https://discourse.julialang.org/u/sgaure)\
**Post date:** [October 29, 2024, 7:58am UTC](https://discourse.julialang.org/t/ccall-and-pointer-to-structure/121884/6 "2024-10-29T07:58:38Z")

</div>

> [@yjr](#):
>
> But if the functions changes the structure, it is not reflected in the julia structure. To my understanding it’s because the pointer is created in the function call thus valid only at function call so the after it is GC and therefore could not alter the content of mydata…  
> Am I correct ?

If the struct isn’t mutable, it can’t change, so julia will assume it hasn’t.
