# Passing a Ptr{Ptr{Int64}} in ccall

**URL:** <https://discourse.julialang.org/t/passing-a-ptr-ptr-int64-in-ccall/82018>\
**Category:** General Usage\
**Tags:** ccall\
**Created:** [May 31, 2022, 10:07pm UTC](https://discourse.julialang.org/t/passing-a-ptr-ptr-int64-in-ccall/82018 "2022-05-31T22:07:09Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dmbates](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dmbates/32/44_2.png) [@dmbates](https://discourse.julialang.org/u/dmbates)\
**Post date:** [May 31, 2022, 10:07pm UTC](https://discourse.julialang.org/t/passing-a-ptr-ptr-int64-in-ccall/82018/1 "2022-05-31T22:07:10Z")

</div>

I have gone down a rabbit hole after reading [https://github.com/lh3/biofast](https://github.com/lh3/biofast) and somehow have ended up writing an interface to the C code in [cgranges](https://github.com/lh3/cgranges). A couple of the functions that I want to call through ccall take an argument that is a `Ptr{Ptr{Int64}}`, allocate storage using malloc within the function call, and populate the vector.

It has been too long since I subjected myself to this sort of task and I can’t remember how to create the object in Julia to be passed through ccall and later deferenced. In C the code looks like

```julia

	int64_t i, n, *b = 0, max_b = 0;
	n = cr_overlap(cr, "chr1", 15, 22, &b, &max_b); // overlap query; output array b[] can be reused
	for (i = 0; i < n; ++i) // traverse overlapping intervals
		printf("%d\t%d\t%d\n", cr_start(cr, b[i]), cr_end(cr, b[i]), cr_label(cr, b[i]));
	free(b); // b[] is allocated by malloc() inside cr_overlap(), so needs to be freed with free()

```

In a Julia function of the form

```nohighlight
function cr_overlap(cr, ctg, st, en, b_, m_b_)
    ccall((:cr_overlap, libcgranges), Int64, (Ptr{cgranges_t}, Ptr{Cchar}, Int32, Int32, Ptr{Ptr{Int64}}, Ptr{Int64}), cr, ctg, st, en, b_, m_b_)
end

```

I can’t figure out what to pass as `b_`.

```julia

```

---

<div class="post-metadata">

**Author:** ![contradict](https://avatars.discourse-cdn.com/v4/letter/c/ac91a4/32.png) [@contradict](https://discourse.julialang.org/u/contradict)\
**Post date:** [May 31, 2022, 11:03pm UTC](https://discourse.julialang.org/t/passing-a-ptr-ptr-int64-in-ccall/82018/2 "2022-05-31T23:03:41Z")

</div>

How about

```julia
b = [Ptr{Int64}(0)]

```

Based on the description [here](https://docs.julialang.org/en/v1/manual/calling-c-and-fortran-code/#man-bits-types) (last paragraph under `Array{T,N}`).

---

<div class="post-metadata">

**Author:** ![torrance](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/torrance/32/38990_2.png) [@torrance](https://discourse.julialang.org/u/torrance)\
**Post date:** [June 1, 2022, 12:11am UTC](https://discourse.julialang.org/t/passing-a-ptr-ptr-int64-in-ccall/82018/3 "2022-06-01T00:11:51Z")

</div>

For this case I’ve used:

```
Ref{Ptr{Int}}()

```

Which can convert to `**int` nicely. It’s basically equivalent to the previous array example but is a little more semantic in my opinion.

---

<div class="post-metadata">

**Author:** ![dmbates](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dmbates/32/44_2.png) [@dmbates](https://discourse.julialang.org/u/dmbates)\
**Post date:** [June 1, 2022, 12:45am UTC](https://discourse.julialang.org/t/passing-a-ptr-ptr-int64-in-ccall/82018/4 "2022-06-01T00:45:50Z")

</div>

Thank you. That works well.
