# Using ccall for C function with out-parameter pointer to pointer to struct

**URL:** <https://discourse.julialang.org/t/using-ccall-for-c-function-with-out-parameter-pointer-to-pointer-to-struct/50999>\
**Category:** General Usage\
**Created:** [November 30, 2020, 4:00pm UTC](https://discourse.julialang.org/t/using-ccall-for-c-function-with-out-parameter-pointer-to-pointer-to-struct/50999 "2020-11-30T16:00:08Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![dmillard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dmillard/32/9448_2.png) [@dmillard](https://discourse.julialang.org/u/dmillard)\
**Post date:** [November 30, 2020, 4:00pm UTC](https://discourse.julialang.org/t/using-ccall-for-c-function-with-out-parameter-pointer-to-pointer-to-struct/50999/1 "2020-11-30T16:00:08Z")

</div>

Hi, I’m wrapping `libfreenect` into Julia. The excellent BinaryBuilder maintainers have helped me get a jll into Yggdrasil, but now I’m working on a more Julian wrapper.

One of the functions I’m wrapping takes a pointer to pointer to struct as an argument. Here’s the relevant parts of the C side.

```julia
typedef enum {
	TILT_STATUS_STOPPED = 0x00, /**< Tilt motor is stopped */
	TILT_STATUS_LIMIT = 0x01, /**< Tilt motor has reached movement limit */
	TILT_STATUS_MOVING = 0x04, /**< Tilt motor is currently moving to new position */
} freenect_tilt_status_code;

/// Data from the tilt motor and accelerometer
typedef struct {
	int16_t accelerometer_x; /**< Raw accelerometer data for X-axis, see FREENECT_COUNTS_PER_G for conversion */
	int16_t accelerometer_y; /**< Raw accelerometer data for Y-axis, see FREENECT_COUNTS_PER_G for conversion */
	int16_t accelerometer_z; /**< Raw accelerometer data for Z-axis, see FREENECT_COUNTS_PER_G for conversion */
	int8_t tilt_angle; /**< Raw tilt motor angle encoder information */
	freenect_tilt_status_code tilt_status; /**< State of the tilt motor (stopped, moving, etc...) */
} freenect_raw_tilt_state;

FREENECTAPI_SYNC int freenect_sync_get_tilt_state(freenect_raw_tilt_state **state, int index);
/* Tilt state function, starts the runloop if it isn't running
    Args:
        state: Populated with an updated tilt state pointer
		    index: Device index (0 is the first)
    Returns:
        Nonzero on error.
*/

```

As you can see, the function `freenect_sync_get_tilt_state` accepts a pointer to pointer to struct, and will provide a struct for me to read at that pointer. From other documentation, this pointer is valid until the next time I call `freenect_sync_get_tilt_state`.

In Julia, I’ve tried to do:

```julia
struct RawTiltState
    accelerometer_x::Cshort
    accelerometer_y::Cshort
    accelerometer_z::Cshort
    tilt_angle::Cchar
    tilt_status::Cchar
end

index = 0
state = Ref{Ptr{RawTiltState}}(C_NULL)
ccall((:freenect_sync_get_tilt_state, :libfreenect_sync), Cint, (Ref{Ptr{RawTiltState}}, Cint), state, index)

```

I can see that the pointer gets populated after this call

```julia
julia> state
Base.RefValue{Ptr{RawTiltState}}(Ptr{RawTiltState} @0x00000000010d9280)

```

But when I try to interpret this as a Julia object, I get a segfault:

```julia
julia> unsafe_pointer_to_objref(state[])

signal (11): Segmentation fault

```

Thanks!

---

<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:** [December 1, 2020, 5:19am UTC](https://discourse.julialang.org/t/using-ccall-for-c-function-with-out-parameter-pointer-to-pointer-to-struct/50999/2 "2020-12-01T05:19:51Z")

</div>

I think you’ll want `unsafe_load(state[])`.

---

<div class="post-metadata">

**Author:** ![dmillard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dmillard/32/9448_2.png) [@dmillard](https://discourse.julialang.org/u/dmillard)\
**Post date:** [December 1, 2020, 5:48am UTC](https://discourse.julialang.org/t/using-ccall-for-c-function-with-out-parameter-pointer-to-pointer-to-struct/50999/3 "2020-12-01T05:48:33Z")

</div>

That was exactly it. Thanks!

---

<div class="post-metadata">

**Author:** ![linanisyugioh](https://avatars.discourse-cdn.com/v4/letter/l/90ced4/32.png) [@linanisyugioh](https://discourse.julialang.org/u/linanisyugioh)\
**Post date:** [March 4, 2022, 3:00pm UTC](https://discourse.julialang.org/t/using-ccall-for-c-function-with-out-parameter-pointer-to-pointer-to-struct/50999/4 "2022-03-04T15:00:36Z")

</div>

freenect\_raw\_tilt\_state \*\*state is a array of pointer to struct freenect\_raw\_tilt\_state. Using unsafe\_load(state) can only get the first element of the array. How to get the other elements ?

---

<div class="post-metadata">

**Author:** ![Gnimuc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gnimuc/32/2194_2.png) [@Gnimuc](https://discourse.julialang.org/u/Gnimuc)\
**Post date:** [March 5, 2022, 12:50am UTC](https://discourse.julialang.org/t/using-ccall-for-c-function-with-out-parameter-pointer-to-pointer-to-struct/50999/5 "2022-03-05T00:50:35Z")

</div>

Using pointer arithmetics or `unsafe_wrap` to load the whole array.
