# Ccall with a C struct containing a pointer

**URL:** <https://discourse.julialang.org/t/ccall-with-a-c-struct-containing-a-pointer/124036>\
**Category:** General Usage\
**Created:** [December 20, 2024, 6:26pm UTC](https://discourse.julialang.org/t/ccall-with-a-c-struct-containing-a-pointer/124036 "2024-12-20T18:26:19Z")\
**Posts on this page:** 1\
**Showing post:** 9

<div class="post-metadata">

**Author:** ![luke-kiernan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/luke-kiernan/32/214309_2.png) [@luke-kiernan](https://discourse.julialang.org/u/luke-kiernan)\
**Post date:** [December 27, 2024, 3:55pm UTC](https://discourse.julialang.org/t/ccall-with-a-c-struct-containing-a-pointer/124036/9 "2024-12-27T15:55:02Z")

</div>

> [@Sukera](#):
>
> That should be something like
> 
> ```julia
> function Base.cconvert...
> 
> ```

Great! Thank you: answer accepted. I’ve expanded that snippet into a self-contained example, for posterity:

```julia
mutable struct DenseMatrix # layout matches the C.
    rowCount::Cint
    columnCount::Cint
    data::Ptr{Cdouble}
end
function Base.cconvert(::Type{DenseMatrix}, m::Matrix{Float64})
    return (m, DenseMatrix(size(m)[1], size(m)[2], C_NULL))
end
function Base.unsafe_convert(::Type{DenseMatrix}, tup::Tuple{Matrix{Float64}, DenseMatrix})
    m, dm = tup
    dm.data = pointer(m) # m is GC-preserved in this function, so this is safe
    return dm
end
function CWrapper(arg1::Matrix{Float64}) # Julia base type here...
    @ccall lib.fname(arg1::DenseMatrix) # ...and C struct type here.
end

```

It’d be good to get a more elaborate example like this in the docs.

---

_[View the full topic](https://discourse.julialang.org/t/ccall-with-a-c-struct-containing-a-pointer/124036)._
