# 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:** 7

<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 24, 2024, 4:38pm UTC](https://discourse.julialang.org/t/ccall-with-a-c-struct-containing-a-pointer/124036/7 "2024-12-24T16:38:51Z")

</div>

Thanks for bringing `cconvert` and `unsafe_convert` to my attention: using those seems like the cleanest way. Storing both the pointer and the pointed-to data could also work…but it feels kind of clunky. However, when I try to imitate the above examples, I’m getting an `Illegal instruction` error.

> [@Sukera](#):
>
> `int printarr(CExample* ex) {return ex->data[0];}`

One thing I should’ve mentioned: the C functions I’m interested in typically take the C struct itself, not a pointer to the C struct. In that case, do things look different than in the above examples?

More details about my goals: I’m trying to build a Julia interface for the Apple Accelerate routines related to sparse matrix factorization. (Related links: my work-in-progress/proof-of-concept [GitHub repo](https://github.com/luke-kiernan/AASparseSolvers.jl/tree/main) and [this forum post](https://discourse.julialang.org/t/apple-accelerate-sparse-solvers/110175).) I want to avoid re-inventing the wheel: ideally, `cconvert` should accept a `SparseMatrixCSC` (the Julia struct) and create a matching [`SparseMatrix`](https://github.com/luke-kiernan/AASparseSolvers.jl/blob/main/src/wrappers.jl#L101) struct (the C struct), for passing into the various `@ccall`s. The structs mostly line up, but there’s small differences: 0- vs 1-indexing, Cints vs Clongs for row indices.

For a more minimal, less involved example: take a look at [`DenseMatrix`](https://github.com/luke-kiernan/AASparseSolvers.jl/blob/main/src/wrappers.jl#L106). This only has a single pointer, and no re-indexing is required. How would I write `cconvert` so as to take a Julia `A::Matrix{T}` and create a `B::DenseMatrix{T}` such that `B.data = pointer(A.ref)`, so I could have [`SparseMultiply`](https://github.com/luke-kiernan/AASparseSolvers.jl/blob/main/src/wrappers.jl#L149) take in normal Julia matrices for `arg2` and `arg3`?

EDIT: okay I have a bigger problem. I’m nesting structs (`SparseMatrixStructure` inside `SparseMatrix`), where the inner struct is not an `isbits` struct type. Thus the `Illegal instruction` error: fields weren’t lining up. But I’d still be interested in knowing the answer to the above question.

---

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